2007-07-11 21:01:13 +04:00
|
|
|
//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for C++ expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2007-08-25 18:02:58 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-10-06 21:59:45 +04:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-08-27 02:59:12 +04:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/Basic/PartialDiagnostic.h"
|
2008-12-03 23:26:15 +03:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-08-27 02:59:12 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2008-12-23 03:26:44 +03:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-11-11 14:37:55 +03:00
|
|
|
/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-11 14:37:55 +03:00
|
|
|
Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
|
|
|
|
bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
|
2009-09-16 02:30:29 +04:00
|
|
|
if (!StdNamespace)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
|
2009-08-19 05:28:28 +04:00
|
|
|
|
|
|
|
if (isType)
|
|
|
|
// FIXME: Preserve type source info.
|
|
|
|
TyOrExpr = GetTypeFromParser(TyOrExpr).getAsOpaquePtr();
|
|
|
|
|
2008-11-20 08:51:55 +03:00
|
|
|
IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
|
2009-10-10 01:13:30 +04:00
|
|
|
LookupResult R;
|
|
|
|
LookupQualifiedName(R, StdNamespace, TypeInfoII, LookupTagName);
|
|
|
|
Decl *TypeInfoDecl = R.getAsSingleDecl(Context);
|
2008-11-11 14:37:55 +03:00
|
|
|
RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
|
2008-11-20 08:51:55 +03:00
|
|
|
if (!TypeInfoRecordDecl)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
|
2008-11-11 14:37:55 +03:00
|
|
|
|
|
|
|
QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
|
|
|
|
|
2009-06-23 00:57:11 +04:00
|
|
|
if (!isType) {
|
|
|
|
// C++0x [expr.typeid]p3:
|
2009-09-09 19:08:12 +04:00
|
|
|
// When typeid is applied to an expression other than an lvalue of a
|
|
|
|
// polymorphic class type [...] [the] expression is an unevaluated
|
2009-06-23 00:57:11 +04:00
|
|
|
// operand.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-23 00:57:11 +04:00
|
|
|
// FIXME: if the type of the expression is a class type, the class
|
|
|
|
// shall be completely defined.
|
|
|
|
bool isUnevaluatedOperand = true;
|
|
|
|
Expr *E = static_cast<Expr *>(TyOrExpr);
|
|
|
|
if (E && !E->isTypeDependent() && E->isLvalue(Context) == Expr::LV_Valid) {
|
|
|
|
QualType T = E->getType();
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const RecordType *RecordT = T->getAs<RecordType>()) {
|
2009-06-23 00:57:11 +04:00
|
|
|
CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl());
|
|
|
|
if (RecordD->isPolymorphic())
|
|
|
|
isUnevaluatedOperand = false;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-23 00:57:11 +04:00
|
|
|
// If this is an unevaluated operand, clear out the set of declaration
|
|
|
|
// references we have been computing.
|
|
|
|
if (isUnevaluatedOperand)
|
|
|
|
PotentiallyReferencedDeclStack.back().clear();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr,
|
|
|
|
TypeInfoType.withConst(),
|
|
|
|
SourceRange(OpLoc, RParenLoc)));
|
2008-11-11 14:37:55 +03:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
/// ActOnCXXBoolLiteral - Parse {true,false} literals.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2007-09-16 18:56:35 +04:00
|
|
|
Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
|
2008-10-24 19:36:09 +04:00
|
|
|
assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
|
2007-07-11 21:01:13 +04:00
|
|
|
"Unknown C++ Boolean value!");
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true,
|
|
|
|
Context.BoolTy, OpLoc));
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-02-26 03:51:44 +03:00
|
|
|
|
2009-05-10 22:38:11 +04:00
|
|
|
/// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
|
|
|
|
Action::OwningExprResult
|
|
|
|
Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) {
|
|
|
|
return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
|
|
|
|
}
|
|
|
|
|
2008-02-26 03:51:44 +03:00
|
|
|
/// ActOnCXXThrow - Parse throw expressions.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
|
|
|
Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) {
|
2009-04-28 00:27:31 +04:00
|
|
|
Expr *Ex = E.takeAs<Expr>();
|
|
|
|
if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex))
|
|
|
|
return ExprError();
|
|
|
|
return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckCXXThrowOperand - Validate the operand of a throw.
|
|
|
|
bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) {
|
|
|
|
// C++ [except.throw]p3:
|
|
|
|
// [...] adjusting the type from "array of T" or "function returning T"
|
|
|
|
// to "pointer to T" or "pointer to function returning T", [...]
|
|
|
|
DefaultFunctionArrayConversion(E);
|
|
|
|
|
|
|
|
// If the type of the exception would be an incomplete type or a pointer
|
|
|
|
// to an incomplete type other than (cv) void the program is ill-formed.
|
|
|
|
QualType Ty = E->getType();
|
|
|
|
int isPointer = 0;
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType* Ptr = Ty->getAs<PointerType>()) {
|
2009-04-28 00:27:31 +04:00
|
|
|
Ty = Ptr->getPointeeType();
|
|
|
|
isPointer = 1;
|
|
|
|
}
|
|
|
|
if (!isPointer || !Ty->isVoidType()) {
|
|
|
|
if (RequireCompleteType(ThrowLoc, Ty,
|
2009-08-27 02:59:12 +04:00
|
|
|
PDiag(isPointer ? diag::err_throw_incomplete_ptr
|
|
|
|
: diag::err_throw_incomplete)
|
|
|
|
<< E->getSourceRange()))
|
2009-04-28 00:27:31 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Construct a temporary here.
|
|
|
|
return false;
|
2008-02-26 03:51:44 +03:00
|
|
|
}
|
2008-07-01 14:37:29 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
|
2008-07-01 14:37:29 +04:00
|
|
|
/// C++ 9.3.2: In the body of a non-static member function, the keyword this
|
|
|
|
/// is a non-lvalue expression whose value is the address of the object for
|
|
|
|
/// which the function is called.
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (!isa<FunctionDecl>(CurContext))
|
|
|
|
return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
|
2008-07-01 14:37:29 +04:00
|
|
|
|
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
|
|
|
|
if (MD->isInstance())
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXThisExpr(ThisLoc,
|
|
|
|
MD->getThisType(Context)));
|
2008-07-01 14:37:29 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
2008-08-22 19:38:55 +04:00
|
|
|
|
|
|
|
/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
|
|
|
|
/// Can be interpreted either as function-style casting ("int(x)")
|
|
|
|
/// or class type construction ("ClassType(x,y,z)")
|
|
|
|
/// or creation of a value-initialized type ("int()").
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-08-22 19:38:55 +04:00
|
|
|
Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
|
|
|
|
SourceLocation LParenLoc,
|
2009-03-15 20:47:39 +03:00
|
|
|
MultiExprArg exprs,
|
2008-08-22 19:38:55 +04:00
|
|
|
SourceLocation *CommaLocs,
|
|
|
|
SourceLocation RParenLoc) {
|
|
|
|
assert(TypeRep && "Missing type!");
|
2009-08-19 05:28:28 +04:00
|
|
|
// FIXME: Preserve type source info.
|
|
|
|
QualType Ty = GetTypeFromParser(TypeRep);
|
2009-03-15 20:47:39 +03:00
|
|
|
unsigned NumExprs = exprs.size();
|
|
|
|
Expr **Exprs = (Expr**)exprs.get();
|
2008-08-22 19:38:55 +04:00
|
|
|
SourceLocation TyBeginLoc = TypeRange.getBegin();
|
|
|
|
SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (Ty->isDependentType() ||
|
2009-03-14 00:01:28 +03:00
|
|
|
CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) {
|
2009-03-15 20:47:39 +03:00
|
|
|
exprs.release();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
return Owned(CXXUnresolvedConstructExpr::Create(Context,
|
|
|
|
TypeRange.getBegin(), Ty,
|
Introduce a new expression type, CXXUnresolvedConstructExpr, to
describe the construction of a value of a given type using function
syntax, e.g.,
T(a1, a2, ..., aN)
when the type or any of its arguments are type-dependent. In this
case, we don't know what kind of type-construction this will be: it
might construct a temporary of type 'T' (which might be a class or
non-class type) or might perform a conversion to type 'T'. Also,
implement printing of and template instantiation for this new
expression type. Due to the change in Sema::ActOnCXXTypeConstructExpr,
our existing tests cover template instantiation of this new expression
node.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72176 91177308-0d34-0410-b5e6-96231b3b80d8
2009-05-20 22:46:25 +04:00
|
|
|
LParenLoc,
|
|
|
|
Exprs, NumExprs,
|
|
|
|
RParenLoc));
|
2009-03-14 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
2009-08-27 07:53:50 +04:00
|
|
|
if (Ty->isArrayType())
|
|
|
|
return ExprError(Diag(TyBeginLoc,
|
|
|
|
diag::err_value_init_for_array_type) << FullRange);
|
|
|
|
if (!Ty->isVoidType() &&
|
|
|
|
RequireCompleteType(TyBeginLoc, Ty,
|
|
|
|
PDiag(diag::err_invalid_incomplete_type_use)
|
|
|
|
<< FullRange))
|
|
|
|
return ExprError();
|
2009-10-24 01:01:39 +04:00
|
|
|
|
2009-08-27 07:53:50 +04:00
|
|
|
if (RequireNonAbstractType(TyBeginLoc, Ty,
|
|
|
|
diag::err_allocation_of_abstract_type))
|
|
|
|
return ExprError();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
// C++ [expr.type.conv]p1:
|
2008-08-22 19:38:55 +04:00
|
|
|
// If the expression list is a single expression, the type conversion
|
|
|
|
// expression is equivalent (in definedness, and if defined in meaning) to the
|
|
|
|
// corresponding cast expression.
|
|
|
|
//
|
|
|
|
if (NumExprs == 1) {
|
2009-08-08 02:21:05 +04:00
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
2009-09-10 01:33:21 +04:00
|
|
|
CXXMethodDecl *Method = 0;
|
|
|
|
if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method,
|
|
|
|
/*FunctionalStyle=*/true))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2009-09-10 01:33:21 +04:00
|
|
|
|
|
|
|
exprs.release();
|
|
|
|
if (Method) {
|
|
|
|
OwningExprResult CastArg
|
|
|
|
= BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(),
|
|
|
|
Kind, Method, Owned(Exprs[0]));
|
|
|
|
if (CastArg.isInvalid())
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
Exprs[0] = CastArg.takeAs<Expr>();
|
2009-08-28 19:11:24 +04:00
|
|
|
}
|
2009-09-10 01:33:21 +04:00
|
|
|
|
|
|
|
return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(),
|
|
|
|
Ty, TyBeginLoc, Kind,
|
|
|
|
Exprs[0], RParenLoc));
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
2009-01-16 21:33:17 +03:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
|
2009-03-15 20:47:39 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
if (NumExprs > 1 || !Record->hasTrivialConstructor() ||
|
2009-08-27 09:08:22 +04:00
|
|
|
!Record->hasTrivialDestructor()) {
|
2009-09-10 03:08:42 +04:00
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
CXXConstructorDecl *Constructor
|
2009-09-10 03:08:42 +04:00
|
|
|
= PerformInitializationByConstructor(Ty, move(exprs),
|
2009-01-16 21:33:17 +03:00
|
|
|
TypeRange.getBegin(),
|
|
|
|
SourceRange(TypeRange.getBegin(),
|
|
|
|
RParenLoc),
|
|
|
|
DeclarationName(),
|
2009-09-10 03:08:42 +04:00
|
|
|
IK_Direct,
|
|
|
|
ConstructorArgs);
|
2009-03-15 20:47:39 +03:00
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
if (!Constructor)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2009-01-16 21:33:17 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
OwningExprResult Result =
|
|
|
|
BuildCXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc,
|
2009-09-10 03:08:42 +04:00
|
|
|
move_arg(ConstructorArgs), RParenLoc);
|
2009-08-27 09:08:22 +04:00
|
|
|
if (Result.isInvalid())
|
|
|
|
return ExprError();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-27 09:08:22 +04:00
|
|
|
return MaybeBindToTemporary(Result.takeAs<Expr>());
|
2009-01-16 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through to value-initialize an object of class type that
|
|
|
|
// doesn't have a user-declared default constructor.
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [expr.type.conv]p1:
|
2008-08-22 19:38:55 +04:00
|
|
|
// If the expression list specifies more than a single value, the type shall
|
|
|
|
// be a class with a suitably declared constructor.
|
|
|
|
//
|
|
|
|
if (NumExprs > 1)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(CommaLocs[0],
|
|
|
|
diag::err_builtin_func_cast_more_than_one_arg)
|
|
|
|
<< FullRange);
|
2008-08-22 19:38:55 +04:00
|
|
|
|
|
|
|
assert(NumExprs == 0 && "Expected 0 expressions");
|
2009-10-27 19:51:19 +03:00
|
|
|
|
|
|
|
if (const RecordType *Record = Ty->getAs<RecordType>()) {
|
|
|
|
if (!Record->getDecl()->isUnion()) {
|
|
|
|
// As clarified in C++ DR302, generate constructor for
|
|
|
|
// value-initialization cases, even if the implementation technique
|
|
|
|
// doesn't call the constructor at that point.
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
(void)PerformInitializationByConstructor(Ty, MultiExprArg(*this, 0, 0),
|
|
|
|
TypeRange.getBegin(),
|
|
|
|
TypeRange, DeclarationName(),
|
|
|
|
IK_Default, ConstructorArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
// C++ [expr.type.conv]p2:
|
2008-08-22 19:38:55 +04:00
|
|
|
// The expression T(), where T is a simple-type-specifier for a non-array
|
|
|
|
// complete object type or the (possibly cv-qualified) void type, creates an
|
|
|
|
// rvalue of the specified type, which is value-initialized.
|
|
|
|
//
|
2009-03-15 20:47:39 +03:00
|
|
|
exprs.release();
|
|
|
|
return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc));
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
2008-09-10 06:17:11 +04:00
|
|
|
|
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
|
|
|
|
/// @code new (memory) int[size][4] @endcode
|
|
|
|
/// or
|
|
|
|
/// @code ::new Foo(23, "hello") @endcode
|
|
|
|
/// For the interpretation of this heap of arguments, consult the base version.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-21 22:14:01 +03:00
|
|
|
Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
|
2009-03-15 20:47:39 +03:00
|
|
|
SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
|
2008-11-21 22:14:01 +03:00
|
|
|
SourceLocation PlacementRParen, bool ParenTypeId,
|
2008-12-02 17:43:59 +03:00
|
|
|
Declarator &D, SourceLocation ConstructorLParen,
|
2009-03-15 20:47:39 +03:00
|
|
|
MultiExprArg ConstructorArgs,
|
2009-09-09 19:08:12 +04:00
|
|
|
SourceLocation ConstructorRParen) {
|
2008-12-02 17:43:59 +03:00
|
|
|
Expr *ArraySize = 0;
|
|
|
|
// If the specified type is an array, unwrap it and save the expression.
|
|
|
|
if (D.getNumTypeObjects() > 0 &&
|
|
|
|
D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
|
|
|
|
DeclaratorChunk &Chunk = D.getTypeObject(0);
|
|
|
|
if (Chunk.Arr.hasStatic)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
|
|
|
|
<< D.getSourceRange());
|
2008-12-02 17:43:59 +03:00
|
|
|
if (!Chunk.Arr.NumElts)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
|
|
|
|
<< D.getSourceRange());
|
2009-10-26 00:45:37 +03:00
|
|
|
|
|
|
|
if (ParenTypeId) {
|
|
|
|
// Can't have dynamic array size when the type-id is in parentheses.
|
|
|
|
Expr *NumElts = (Expr *)Chunk.Arr.NumElts;
|
|
|
|
if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() &&
|
|
|
|
!NumElts->isIntegerConstantExpr(Context)) {
|
|
|
|
Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst)
|
|
|
|
<< NumElts->getSourceRange();
|
|
|
|
return ExprError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-02 17:43:59 +03:00
|
|
|
ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
|
2009-10-26 00:45:37 +03:00
|
|
|
D.DropFirstTypeObject();
|
2008-12-02 17:43:59 +03:00
|
|
|
}
|
|
|
|
|
2009-09-11 04:18:58 +04:00
|
|
|
// Every dimension shall be of constant size.
|
2009-10-26 00:45:37 +03:00
|
|
|
if (ArraySize) {
|
|
|
|
for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) {
|
2009-09-11 04:18:58 +04:00
|
|
|
if (D.getTypeObject(I).Kind != DeclaratorChunk::Array)
|
|
|
|
break;
|
|
|
|
|
|
|
|
DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr;
|
|
|
|
if (Expr *NumElts = (Expr *)Array.NumElts) {
|
|
|
|
if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() &&
|
|
|
|
!NumElts->isIntegerConstantExpr(Context)) {
|
|
|
|
Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst)
|
|
|
|
<< NumElts->getSourceRange();
|
|
|
|
return ExprError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-26 00:45:37 +03:00
|
|
|
|
2009-08-19 05:27:57 +04:00
|
|
|
//FIXME: Store DeclaratorInfo in CXXNew expression.
|
|
|
|
DeclaratorInfo *DInfo = 0;
|
2009-10-26 00:45:37 +03:00
|
|
|
QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &DInfo);
|
2009-04-25 12:06:05 +04:00
|
|
|
if (D.isInvalidType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-12-02 17:43:59 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
return BuildCXXNew(StartLoc, UseGlobal,
|
2009-05-21 04:00:09 +04:00
|
|
|
PlacementLParen,
|
2009-09-09 19:08:12 +04:00
|
|
|
move(PlacementArgs),
|
2009-05-21 04:00:09 +04:00
|
|
|
PlacementRParen,
|
|
|
|
ParenTypeId,
|
2009-09-09 19:08:12 +04:00
|
|
|
AllocType,
|
2009-05-21 04:00:09 +04:00
|
|
|
D.getSourceRange().getBegin(),
|
|
|
|
D.getSourceRange(),
|
|
|
|
Owned(ArraySize),
|
|
|
|
ConstructorLParen,
|
|
|
|
move(ConstructorArgs),
|
|
|
|
ConstructorRParen);
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::OwningExprResult
|
2009-05-21 04:00:09 +04:00
|
|
|
Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
|
|
|
|
SourceLocation PlacementLParen,
|
|
|
|
MultiExprArg PlacementArgs,
|
|
|
|
SourceLocation PlacementRParen,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool ParenTypeId,
|
2009-05-21 04:00:09 +04:00
|
|
|
QualType AllocType,
|
|
|
|
SourceLocation TypeLoc,
|
|
|
|
SourceRange TypeRange,
|
|
|
|
ExprArg ArraySizeE,
|
|
|
|
SourceLocation ConstructorLParen,
|
|
|
|
MultiExprArg ConstructorArgs,
|
|
|
|
SourceLocation ConstructorRParen) {
|
|
|
|
if (CheckAllocatedType(AllocType, TypeLoc, TypeRange))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-05-21 04:00:09 +04:00
|
|
|
QualType ResultType = Context.getPointerType(AllocType);
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
// That every array dimension except the first is constant was already
|
|
|
|
// checked by the type check above.
|
2008-12-02 17:43:59 +03:00
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
// C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
|
|
|
|
// or enumeration type with a non-negative value."
|
2009-05-21 04:00:09 +04:00
|
|
|
Expr *ArraySize = (Expr *)ArraySizeE.get();
|
2009-02-26 17:39:58 +03:00
|
|
|
if (ArraySize && !ArraySize->isTypeDependent()) {
|
2008-12-02 17:43:59 +03:00
|
|
|
QualType SizeType = ArraySize->getType();
|
|
|
|
if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
|
|
|
|
diag::err_array_size_not_integral)
|
|
|
|
<< SizeType << ArraySize->getSourceRange());
|
2008-12-02 17:43:59 +03:00
|
|
|
// Let's see if this is a constant < 0. If so, we reject it out of hand.
|
|
|
|
// We don't care about special rules, so we tell the machinery it's not
|
|
|
|
// evaluated - it gives us a result in more cases.
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!ArraySize->isValueDependent()) {
|
|
|
|
llvm::APSInt Value;
|
|
|
|
if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
|
|
|
|
if (Value < llvm::APSInt(
|
2009-09-23 04:37:25 +04:00
|
|
|
llvm::APInt::getNullValue(Value.getBitWidth()),
|
|
|
|
Value.isUnsigned()))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
|
|
|
|
diag::err_typecheck_negative_array_size)
|
|
|
|
<< ArraySize->getSourceRange());
|
2009-02-26 17:39:58 +03:00
|
|
|
}
|
2008-12-02 17:43:59 +03:00
|
|
|
}
|
2009-09-23 04:37:25 +04:00
|
|
|
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(ArraySize, Context.getSizeType(),
|
|
|
|
CastExpr::CK_IntegralCast);
|
2008-12-02 17:43:59 +03:00
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
FunctionDecl *OperatorNew = 0;
|
|
|
|
FunctionDecl *OperatorDelete = 0;
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr **PlaceArgs = (Expr**)PlacementArgs.get();
|
|
|
|
unsigned NumPlaceArgs = PlacementArgs.size();
|
2009-10-18 01:40:42 +04:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!AllocType->isDependentType() &&
|
|
|
|
!Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) &&
|
|
|
|
FindAllocationFunctions(StartLoc,
|
2009-02-09 21:24:27 +03:00
|
|
|
SourceRange(PlacementLParen, PlacementRParen),
|
|
|
|
UseGlobal, AllocType, ArraySize, PlaceArgs,
|
|
|
|
NumPlaceArgs, OperatorNew, OperatorDelete))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
bool Init = ConstructorLParen.isValid();
|
|
|
|
// --- Choosing a constructor ---
|
|
|
|
// C++ 5.3.4p15
|
|
|
|
// 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
|
|
|
|
// the object is not initialized. If the object, or any part of it, is
|
|
|
|
// const-qualified, it's an error.
|
|
|
|
// 2) If T is a POD and there's an empty initializer, the object is value-
|
|
|
|
// initialized.
|
|
|
|
// 3) If T is a POD and there's one initializer argument, the object is copy-
|
|
|
|
// constructed.
|
|
|
|
// 4) If T is a POD and there's more initializer arguments, it's an error.
|
|
|
|
// 5) If T is not a POD, the initializer arguments are used as constructor
|
|
|
|
// arguments.
|
|
|
|
//
|
|
|
|
// Or by the C++0x formulation:
|
|
|
|
// 1) If there's no initializer, the object is default-initialized according
|
|
|
|
// to C++0x rules.
|
|
|
|
// 2) Otherwise, the object is direct-initialized.
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr **ConsArgs = (Expr**)ConstructorArgs.get();
|
2009-05-07 20:14:23 +04:00
|
|
|
const RecordType *RT;
|
2009-03-15 20:47:39 +03:00
|
|
|
unsigned NumConsArgs = ConstructorArgs.size();
|
2009-10-18 01:40:42 +04:00
|
|
|
|
|
|
|
if (AllocType->isDependentType() ||
|
|
|
|
Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) {
|
2009-02-26 17:39:58 +03:00
|
|
|
// Skip all the checks.
|
2009-08-05 01:02:39 +04:00
|
|
|
} else if ((RT = AllocType->getAs<RecordType>()) &&
|
|
|
|
!AllocType->isAggregateType()) {
|
2009-09-10 03:08:42 +04:00
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this);
|
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
Constructor = PerformInitializationByConstructor(
|
2009-09-10 03:08:42 +04:00
|
|
|
AllocType, move(ConstructorArgs),
|
2009-05-21 04:00:09 +04:00
|
|
|
TypeLoc,
|
|
|
|
SourceRange(TypeLoc, ConstructorRParen),
|
2008-11-24 08:29:24 +03:00
|
|
|
RT->getDecl()->getDeclName(),
|
2009-09-10 03:08:42 +04:00
|
|
|
NumConsArgs != 0 ? IK_Direct : IK_Default,
|
|
|
|
ConvertedConstructorArgs);
|
2008-11-21 22:14:01 +03:00
|
|
|
if (!Constructor)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2009-09-10 03:08:42 +04:00
|
|
|
|
|
|
|
// Take the converted constructor arguments and use them for the new
|
|
|
|
// expression.
|
|
|
|
NumConsArgs = ConvertedConstructorArgs.size();
|
|
|
|
ConsArgs = (Expr **)ConvertedConstructorArgs.take();
|
2008-11-21 22:14:01 +03:00
|
|
|
} else {
|
|
|
|
if (!Init) {
|
|
|
|
// FIXME: Check that no subpart is const.
|
2009-03-15 20:47:39 +03:00
|
|
|
if (AllocType.isConstQualified())
|
|
|
|
return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const)
|
2009-05-21 04:00:09 +04:00
|
|
|
<< TypeRange);
|
2008-11-21 22:14:01 +03:00
|
|
|
} else if (NumConsArgs == 0) {
|
2009-10-27 19:51:19 +03:00
|
|
|
// Object is value-initialized.
|
|
|
|
if (const RecordType *Record = AllocType->getAs<RecordType>()) {
|
|
|
|
if (!Record->getDecl()->isUnion()) {
|
|
|
|
// As clarified in C++ DR302, generate constructor for
|
|
|
|
// value-initialization cases, even if the implementation technique
|
|
|
|
// doesn't call the constructor at that point.
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
(void)PerformInitializationByConstructor(AllocType,
|
|
|
|
MultiExprArg(*this, 0, 0),
|
|
|
|
TypeRange.getBegin(),
|
|
|
|
TypeRange, DeclarationName(),
|
|
|
|
IK_Default,
|
|
|
|
ConstructorArgs);
|
|
|
|
}
|
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
} else if (NumConsArgs == 1) {
|
|
|
|
// Object is direct-initialized.
|
2009-05-07 20:14:23 +04:00
|
|
|
// FIXME: What DeclarationName do we pass in here?
|
2008-12-02 17:43:59 +03:00
|
|
|
if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
|
2009-01-14 18:45:31 +03:00
|
|
|
DeclarationName() /*AllocType.getAsString()*/,
|
|
|
|
/*DirectInit=*/true))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
} else {
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(StartLoc,
|
|
|
|
diag::err_builtin_direct_init_more_than_one_arg)
|
|
|
|
<< SourceRange(ConstructorLParen, ConstructorRParen));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
|
2009-10-18 01:40:42 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
PlacementArgs.release();
|
|
|
|
ConstructorArgs.release();
|
2009-05-21 04:00:09 +04:00
|
|
|
ArraySizeE.release();
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs,
|
2009-02-07 04:47:29 +03:00
|
|
|
NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init,
|
2008-11-21 22:14:01 +03:00
|
|
|
ConsArgs, NumConsArgs, OperatorDelete, ResultType,
|
2009-09-09 19:08:12 +04:00
|
|
|
StartLoc, Init ? ConstructorRParen : SourceLocation()));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckAllocatedType - Checks that a type is suitable as the allocated type
|
|
|
|
/// in a new-expression.
|
|
|
|
/// dimension off and stores the size expression in ArraySize.
|
2009-05-21 04:00:09 +04:00
|
|
|
bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
|
2009-09-09 19:08:12 +04:00
|
|
|
SourceRange R) {
|
2008-11-21 22:14:01 +03:00
|
|
|
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
|
|
|
|
// abstract class type or array thereof.
|
2009-03-24 22:52:54 +03:00
|
|
|
if (AllocType->isFunctionType())
|
2009-05-21 04:00:09 +04:00
|
|
|
return Diag(Loc, diag::err_bad_new_type)
|
|
|
|
<< AllocType << 0 << R;
|
2009-03-24 22:52:54 +03:00
|
|
|
else if (AllocType->isReferenceType())
|
2009-05-21 04:00:09 +04:00
|
|
|
return Diag(Loc, diag::err_bad_new_type)
|
|
|
|
<< AllocType << 1 << R;
|
2009-03-24 22:52:54 +03:00
|
|
|
else if (!AllocType->isDependentType() &&
|
2009-05-21 04:00:09 +04:00
|
|
|
RequireCompleteType(Loc, AllocType,
|
2009-08-27 03:45:07 +04:00
|
|
|
PDiag(diag::err_new_incomplete_type)
|
|
|
|
<< R))
|
2009-03-24 22:52:54 +03:00
|
|
|
return true;
|
2009-05-21 04:00:09 +04:00
|
|
|
else if (RequireNonAbstractType(Loc, AllocType,
|
2009-03-24 22:52:54 +03:00
|
|
|
diag::err_allocation_of_abstract_type))
|
2008-11-21 22:14:01 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:26:15 +03:00
|
|
|
/// FindAllocationFunctions - Finds the overloads of operator new and delete
|
|
|
|
/// that are appropriate for the allocation.
|
2009-02-09 21:24:27 +03:00
|
|
|
bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
|
|
|
|
bool UseGlobal, QualType AllocType,
|
|
|
|
bool IsArray, Expr **PlaceArgs,
|
|
|
|
unsigned NumPlaceArgs,
|
2008-12-03 23:26:15 +03:00
|
|
|
FunctionDecl *&OperatorNew,
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionDecl *&OperatorDelete) {
|
2008-12-03 23:26:15 +03:00
|
|
|
// --- Choosing an allocation function ---
|
|
|
|
// C++ 5.3.4p8 - 14 & 18
|
|
|
|
// 1) If UseGlobal is true, only look in the global scope. Else, also look
|
|
|
|
// in the scope of the allocated class.
|
|
|
|
// 2) If an array size is given, look for operator new[], else look for
|
|
|
|
// operator new.
|
|
|
|
// 3) The first argument is always size_t. Append the arguments from the
|
|
|
|
// placement form.
|
|
|
|
// FIXME: Also find the appropriate delete operator.
|
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
|
|
|
|
// We don't care about the actual value of this argument.
|
|
|
|
// FIXME: Should the Sema create the expression and embed it in the syntax
|
|
|
|
// tree? Or should the consumer just recalculate the value?
|
2009-08-17 00:29:29 +04:00
|
|
|
IntegerLiteral Size(llvm::APInt::getNullValue(
|
|
|
|
Context.Target.getPointerWidth(0)),
|
|
|
|
Context.getSizeType(),
|
|
|
|
SourceLocation());
|
|
|
|
AllocArgs[0] = &Size;
|
2008-12-03 23:26:15 +03:00
|
|
|
std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
|
|
|
|
|
|
|
|
DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
|
|
|
|
IsArray ? OO_Array_New : OO_New);
|
|
|
|
if (AllocType->isRecordType() && !UseGlobal) {
|
2009-09-09 19:08:12 +04:00
|
|
|
CXXRecordDecl *Record
|
2009-07-30 01:53:49 +04:00
|
|
|
= cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl());
|
2008-12-05 01:20:51 +03:00
|
|
|
// FIXME: We fail to find inherited overloads.
|
2009-02-09 21:24:27 +03:00
|
|
|
if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
|
2008-12-05 01:20:51 +03:00
|
|
|
AllocArgs.size(), Record, /*AllowMissing=*/true,
|
|
|
|
OperatorNew))
|
2008-12-03 23:26:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!OperatorNew) {
|
|
|
|
// Didn't find a member overload. Look for a global one.
|
|
|
|
DeclareGlobalNewDelete();
|
2008-12-05 01:20:51 +03:00
|
|
|
DeclContext *TUDecl = Context.getTranslationUnitDecl();
|
2009-02-09 21:24:27 +03:00
|
|
|
if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
|
2008-12-05 01:20:51 +03:00
|
|
|
AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
|
|
|
|
OperatorNew))
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2009-06-01 00:26:12 +04:00
|
|
|
// FindAllocationOverload can change the passed in arguments, so we need to
|
|
|
|
// copy them back.
|
|
|
|
if (NumPlaceArgs > 0)
|
|
|
|
std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
/// FindAllocationOverload - Find an fitting overload for the allocation
|
|
|
|
/// function in the specified scope.
|
2009-02-09 21:24:27 +03:00
|
|
|
bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
|
|
|
|
DeclarationName Name, Expr** Args,
|
|
|
|
unsigned NumArgs, DeclContext *Ctx,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool AllowMissing, FunctionDecl *&Operator) {
|
2009-10-10 01:13:30 +04:00
|
|
|
LookupResult R;
|
|
|
|
LookupQualifiedName(R, Ctx, Name, LookupOrdinaryName);
|
|
|
|
if (R.empty()) {
|
2008-12-05 01:20:51 +03:00
|
|
|
if (AllowMissing)
|
|
|
|
return false;
|
|
|
|
return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
|
2009-02-17 10:29:20 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2009-10-10 01:13:30 +04:00
|
|
|
// FIXME: handle ambiguity
|
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
OverloadCandidateSet Candidates;
|
2009-09-30 04:03:47 +04:00
|
|
|
for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end();
|
|
|
|
Alloc != AllocEnd; ++Alloc) {
|
2008-12-23 03:26:44 +03:00
|
|
|
// Even member operator new/delete are implicitly treated as
|
|
|
|
// static, so don't use AddMemberCandidate.
|
2009-09-29 22:16:17 +04:00
|
|
|
if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) {
|
2008-12-23 03:26:44 +03:00
|
|
|
AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
|
|
|
|
/*SuppressUserConversions=*/false);
|
2009-09-29 22:16:17 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle function templates
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
// Do the resolution.
|
|
|
|
OverloadCandidateSet::iterator Best;
|
2009-06-20 03:52:42 +04:00
|
|
|
switch(BestViableFunction(Candidates, StartLoc, Best)) {
|
2008-12-05 01:20:51 +03:00
|
|
|
case OR_Success: {
|
|
|
|
// Got one!
|
|
|
|
FunctionDecl *FnDecl = Best->Function;
|
|
|
|
// The first argument is size_t, and the first parameter must be size_t,
|
|
|
|
// too. This is checked on declaration and can be assumed. (It can't be
|
|
|
|
// asserted on, though, since invalid decls are left in there.)
|
2009-09-29 22:16:17 +04:00
|
|
|
for (unsigned i = 0; i < NumArgs; ++i) {
|
2008-12-05 01:20:51 +03:00
|
|
|
// FIXME: Passing word to diagnostic.
|
2009-05-31 23:49:47 +04:00
|
|
|
if (PerformCopyInitialization(Args[i],
|
2008-12-05 01:20:51 +03:00
|
|
|
FnDecl->getParamDecl(i)->getType(),
|
|
|
|
"passing"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Operator = FnDecl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OR_No_Viable_Function:
|
|
|
|
Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
|
2009-02-17 10:29:20 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case OR_Ambiguous:
|
|
|
|
Diag(StartLoc, diag::err_ovl_ambiguous_call)
|
2009-02-09 21:24:27 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
|
|
|
|
return true;
|
2009-02-19 00:56:37 +03:00
|
|
|
|
|
|
|
case OR_Deleted:
|
|
|
|
Diag(StartLoc, diag::err_ovl_deleted_call)
|
|
|
|
<< Best->Function->isDeleted()
|
|
|
|
<< Name << Range;
|
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
|
|
|
|
return true;
|
2008-12-05 01:20:51 +03:00
|
|
|
}
|
|
|
|
assert(false && "Unreachable, bad result from BestViableFunction");
|
|
|
|
return true;
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
|
2008-12-03 23:26:15 +03:00
|
|
|
/// DeclareGlobalNewDelete - Declare the global forms of operator new and
|
|
|
|
/// delete. These are:
|
|
|
|
/// @code
|
|
|
|
/// void* operator new(std::size_t) throw(std::bad_alloc);
|
|
|
|
/// void* operator new[](std::size_t) throw(std::bad_alloc);
|
|
|
|
/// void operator delete(void *) throw();
|
|
|
|
/// void operator delete[](void *) throw();
|
|
|
|
/// @endcode
|
|
|
|
/// Note that the placement and nothrow forms of new are *not* implicitly
|
|
|
|
/// declared. Their use requires including \<new\>.
|
2009-09-09 19:08:12 +04:00
|
|
|
void Sema::DeclareGlobalNewDelete() {
|
2008-12-03 23:26:15 +03:00
|
|
|
if (GlobalNewDeleteDeclared)
|
|
|
|
return;
|
2009-09-16 02:30:29 +04:00
|
|
|
|
|
|
|
// C++ [basic.std.dynamic]p2:
|
|
|
|
// [...] The following allocation and deallocation functions (18.4) are
|
|
|
|
// implicitly declared in global scope in each translation unit of a
|
|
|
|
// program
|
|
|
|
//
|
|
|
|
// void* operator new(std::size_t) throw(std::bad_alloc);
|
|
|
|
// void* operator new[](std::size_t) throw(std::bad_alloc);
|
|
|
|
// void operator delete(void*) throw();
|
|
|
|
// void operator delete[](void*) throw();
|
|
|
|
//
|
|
|
|
// These implicit declarations introduce only the function names operator
|
|
|
|
// new, operator new[], operator delete, operator delete[].
|
|
|
|
//
|
|
|
|
// Here, we need to refer to std::bad_alloc, so we will implicitly declare
|
|
|
|
// "std" or "bad_alloc" as necessary to form the exception specification.
|
|
|
|
// However, we do not make these implicit declarations visible to name
|
|
|
|
// lookup.
|
|
|
|
if (!StdNamespace) {
|
|
|
|
// The "std" namespace has not yet been defined, so build one implicitly.
|
|
|
|
StdNamespace = NamespaceDecl::Create(Context,
|
|
|
|
Context.getTranslationUnitDecl(),
|
|
|
|
SourceLocation(),
|
|
|
|
&PP.getIdentifierTable().get("std"));
|
|
|
|
StdNamespace->setImplicit(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StdBadAlloc) {
|
|
|
|
// The "std::bad_alloc" class has not yet been declared, so build it
|
|
|
|
// implicitly.
|
|
|
|
StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class,
|
|
|
|
StdNamespace,
|
|
|
|
SourceLocation(),
|
|
|
|
&PP.getIdentifierTable().get("bad_alloc"),
|
|
|
|
SourceLocation(), 0);
|
|
|
|
StdBadAlloc->setImplicit(true);
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:26:15 +03:00
|
|
|
GlobalNewDeleteDeclared = true;
|
|
|
|
|
|
|
|
QualType VoidPtr = Context.getPointerType(Context.VoidTy);
|
|
|
|
QualType SizeT = Context.getSizeType();
|
|
|
|
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_New),
|
|
|
|
VoidPtr, SizeT);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
|
|
|
|
VoidPtr, SizeT);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Delete),
|
|
|
|
Context.VoidTy, VoidPtr);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
|
|
|
|
Context.VoidTy, VoidPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeclareGlobalAllocationFunction - Declares a single implicit global
|
|
|
|
/// allocation function if it doesn't already exist.
|
|
|
|
void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType Return, QualType Argument) {
|
2008-12-03 23:26:15 +03:00
|
|
|
DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
|
|
|
|
|
|
|
|
// Check if this function is already declared.
|
2008-12-24 00:05:05 +03:00
|
|
|
{
|
2008-12-24 01:05:29 +03:00
|
|
|
DeclContext::lookup_iterator Alloc, AllocEnd;
|
2009-06-30 06:36:12 +04:00
|
|
|
for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
|
2008-12-24 00:05:05 +03:00
|
|
|
Alloc != AllocEnd; ++Alloc) {
|
|
|
|
// FIXME: Do we need to check for default arguments here?
|
|
|
|
FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
|
|
|
|
if (Func->getNumParams() == 1 &&
|
2009-02-07 04:47:29 +03:00
|
|
|
Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument)
|
2008-12-03 23:26:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 02:30:29 +04:00
|
|
|
QualType BadAllocType;
|
|
|
|
bool HasBadAllocExceptionSpec
|
|
|
|
= (Name.getCXXOverloadedOperator() == OO_New ||
|
|
|
|
Name.getCXXOverloadedOperator() == OO_Array_New);
|
|
|
|
if (HasBadAllocExceptionSpec) {
|
|
|
|
assert(StdBadAlloc && "Must have std::bad_alloc declared");
|
|
|
|
BadAllocType = Context.getTypeDeclType(StdBadAlloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0,
|
|
|
|
true, false,
|
|
|
|
HasBadAllocExceptionSpec? 1 : 0,
|
|
|
|
&BadAllocType);
|
2008-12-03 23:26:15 +03:00
|
|
|
FunctionDecl *Alloc =
|
|
|
|
FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
|
2009-08-21 04:31:54 +04:00
|
|
|
FnType, /*DInfo=*/0, FunctionDecl::None, false, true);
|
2008-12-03 23:26:15 +03:00
|
|
|
Alloc->setImplicit();
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
|
2009-08-19 05:27:57 +04:00
|
|
|
0, Argument, /*DInfo=*/0,
|
|
|
|
VarDecl::None, 0);
|
2009-01-14 03:42:25 +03:00
|
|
|
Alloc->setParams(Context, &Param, 1);
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-24 00:05:05 +03:00
|
|
|
// FIXME: Also add this declaration to the IdentifierResolver, but
|
|
|
|
// make sure it is at the end of the chain to coincide with the
|
|
|
|
// global scope.
|
2009-06-30 06:36:12 +04:00
|
|
|
((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
|
|
|
|
/// @code ::delete ptr; @endcode
|
|
|
|
/// or
|
|
|
|
/// @code delete [] ptr; @endcode
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-21 22:14:01 +03:00
|
|
|
Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool ArrayForm, ExprArg Operand) {
|
2009-09-10 03:39:55 +04:00
|
|
|
// C++ [expr.delete]p1:
|
|
|
|
// The operand shall have a pointer type, or a class type having a single
|
|
|
|
// conversion function to a pointer type. The result has type void.
|
|
|
|
//
|
2008-11-21 22:14:01 +03:00
|
|
|
// DR599 amends "pointer type" to "pointer to object type" in both cases.
|
|
|
|
|
2009-08-17 00:29:29 +04:00
|
|
|
FunctionDecl *OperatorDelete = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr *Ex = (Expr *)Operand.get();
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!Ex->isTypeDependent()) {
|
|
|
|
QualType Type = Ex->getType();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-09-10 03:39:55 +04:00
|
|
|
if (const RecordType *Record = Type->getAs<RecordType>()) {
|
|
|
|
llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions;
|
2009-09-12 01:44:33 +04:00
|
|
|
CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
|
|
|
|
OverloadedFunctionDecl *Conversions =
|
2009-09-12 22:26:03 +04:00
|
|
|
RD->getVisibleConversionFunctions();
|
2009-09-10 03:39:55 +04:00
|
|
|
|
|
|
|
for (OverloadedFunctionDecl::function_iterator
|
|
|
|
Func = Conversions->function_begin(),
|
|
|
|
FuncEnd = Conversions->function_end();
|
|
|
|
Func != FuncEnd; ++Func) {
|
|
|
|
// Skip over templated conversion functions; they aren't considered.
|
|
|
|
if (isa<FunctionTemplateDecl>(*Func))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
|
|
|
|
|
|
|
|
QualType ConvType = Conv->getConversionType().getNonReferenceType();
|
|
|
|
if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
|
|
|
|
if (ConvPtrType->getPointeeType()->isObjectType())
|
2009-09-16 02:15:23 +04:00
|
|
|
ObjectPtrConversions.push_back(Conv);
|
2009-09-10 03:39:55 +04:00
|
|
|
}
|
2009-09-16 02:15:23 +04:00
|
|
|
if (ObjectPtrConversions.size() == 1) {
|
|
|
|
// We have a single conversion to a pointer-to-object type. Perform
|
|
|
|
// that conversion.
|
|
|
|
Operand.release();
|
|
|
|
if (!PerformImplicitConversion(Ex,
|
|
|
|
ObjectPtrConversions.front()->getConversionType(),
|
|
|
|
"converting")) {
|
|
|
|
Operand = Owned(Ex);
|
|
|
|
Type = Ex->getType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ObjectPtrConversions.size() > 1) {
|
|
|
|
Diag(StartLoc, diag::err_ambiguous_delete_operand)
|
|
|
|
<< Type << Ex->getSourceRange();
|
|
|
|
for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) {
|
|
|
|
CXXConversionDecl *Conv = ObjectPtrConversions[i];
|
|
|
|
Diag(Conv->getLocation(), diag::err_ovl_candidate);
|
|
|
|
}
|
|
|
|
return ExprError();
|
2009-09-10 03:39:55 +04:00
|
|
|
}
|
2009-02-26 17:39:58 +03:00
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (!Type->isPointerType())
|
|
|
|
return ExprError(Diag(StartLoc, diag::err_delete_operand)
|
|
|
|
<< Type << Ex->getSourceRange());
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
QualType Pointee = Type->getAs<PointerType>()->getPointeeType();
|
2009-03-24 23:13:58 +03:00
|
|
|
if (Pointee->isFunctionType() || Pointee->isVoidType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(StartLoc, diag::err_delete_operand)
|
|
|
|
<< Type << Ex->getSourceRange());
|
2009-03-24 23:13:58 +03:00
|
|
|
else if (!Pointee->isDependentType() &&
|
2009-09-09 19:08:12 +04:00
|
|
|
RequireCompleteType(StartLoc, Pointee,
|
2009-08-27 03:45:07 +04:00
|
|
|
PDiag(diag::warn_delete_incomplete)
|
|
|
|
<< Ex->getSourceRange()))
|
2009-03-24 23:13:58 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-09-30 01:38:53 +04:00
|
|
|
// C++ [expr.delete]p2:
|
|
|
|
// [Note: a pointer to a const type can be the operand of a
|
|
|
|
// delete-expression; it is not necessary to cast away the constness
|
|
|
|
// (5.2.11) of the pointer expression before it is used as the operand
|
|
|
|
// of the delete-expression. ]
|
|
|
|
ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy),
|
|
|
|
CastExpr::CK_NoOp);
|
|
|
|
|
|
|
|
// Update the operand.
|
|
|
|
Operand.take();
|
|
|
|
Operand = ExprArg(*this, Ex);
|
|
|
|
|
2009-08-17 00:29:29 +04:00
|
|
|
DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
|
|
|
|
ArrayForm ? OO_Array_Delete : OO_Delete);
|
|
|
|
|
|
|
|
if (Pointee->isRecordType() && !UseGlobal) {
|
2009-09-09 19:08:12 +04:00
|
|
|
CXXRecordDecl *Record
|
2009-08-17 00:29:29 +04:00
|
|
|
= cast<CXXRecordDecl>(Pointee->getAs<RecordType>()->getDecl());
|
2009-09-29 22:16:17 +04:00
|
|
|
|
|
|
|
// Try to find operator delete/operator delete[] in class scope.
|
2009-10-10 01:13:30 +04:00
|
|
|
LookupResult Found;
|
|
|
|
LookupQualifiedName(Found, Record, DeleteName, LookupOrdinaryName);
|
2009-09-29 22:16:17 +04:00
|
|
|
// FIXME: Diagnose ambiguity properly
|
|
|
|
assert(!Found.isAmbiguous() && "Ambiguous delete/delete[] not handled");
|
|
|
|
for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
|
|
|
|
F != FEnd; ++F) {
|
|
|
|
if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F))
|
|
|
|
if (Delete->isUsualDeallocationFunction()) {
|
|
|
|
OperatorDelete = Delete;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 03:18:17 +04:00
|
|
|
if (!Record->hasTrivialDestructor())
|
|
|
|
if (const CXXDestructorDecl *Dtor = Record->getDestructor(Context))
|
2009-09-09 19:08:12 +04:00
|
|
|
MarkDeclarationReferenced(StartLoc,
|
2009-09-04 03:18:17 +04:00
|
|
|
const_cast<CXXDestructorDecl*>(Dtor));
|
2009-08-17 00:29:29 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-17 00:29:29 +04:00
|
|
|
if (!OperatorDelete) {
|
|
|
|
// Didn't find a member overload. Look for a global one.
|
|
|
|
DeclareGlobalNewDelete();
|
|
|
|
DeclContext *TUDecl = Context.getTranslationUnitDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName,
|
2009-09-29 22:16:17 +04:00
|
|
|
&Ex, 1, TUDecl, /*AllowMissing=*/false,
|
2009-08-17 00:29:29 +04:00
|
|
|
OperatorDelete))
|
|
|
|
return ExprError();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
// FIXME: Check access and ambiguity of operator delete and destructor.
|
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Operand.release();
|
|
|
|
return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
|
2009-08-17 00:29:29 +04:00
|
|
|
OperatorDelete, Ex, StartLoc));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-10 06:17:11 +04:00
|
|
|
/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
|
|
|
|
/// C++ if/switch/while/for statement.
|
|
|
|
/// e.g: "if (int x = f()) {...}"
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-09-10 06:17:11 +04:00
|
|
|
Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
|
|
|
|
Declarator &D,
|
|
|
|
SourceLocation EqualLoc,
|
2009-03-15 20:47:39 +03:00
|
|
|
ExprArg AssignExprVal) {
|
|
|
|
assert(AssignExprVal.get() && "Null assignment expression");
|
2008-09-10 06:17:11 +04:00
|
|
|
|
|
|
|
// C++ 6.4p2:
|
|
|
|
// The declarator shall not specify a function or an array.
|
|
|
|
// The type-specifier-seq shall not contain typedef and shall not declare a
|
|
|
|
// new class or enumeration.
|
|
|
|
|
|
|
|
assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
|
|
|
|
"Parser allowed 'typedef' as storage class of condition decl.");
|
|
|
|
|
2009-08-19 05:27:57 +04:00
|
|
|
// FIXME: Store DeclaratorInfo in the expression.
|
|
|
|
DeclaratorInfo *DInfo = 0;
|
2009-08-11 09:20:41 +04:00
|
|
|
TagDecl *OwnedTag = 0;
|
2009-10-26 00:45:37 +03:00
|
|
|
QualType Ty = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-10 06:17:11 +04:00
|
|
|
if (Ty->isFunctionType()) { // The declarator shall not specify a function...
|
|
|
|
// We exit without creating a CXXConditionDeclExpr because a FunctionDecl
|
|
|
|
// would be created and CXXConditionDeclExpr wants a VarDecl.
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type)
|
|
|
|
<< SourceRange(StartLoc, EqualLoc));
|
2008-09-10 06:17:11 +04:00
|
|
|
} else if (Ty->isArrayType()) { // ...or an array.
|
2008-11-19 08:27:50 +03:00
|
|
|
Diag(StartLoc, diag::err_invalid_use_of_array_type)
|
|
|
|
<< SourceRange(StartLoc, EqualLoc);
|
2009-08-11 09:20:41 +04:00
|
|
|
} else if (OwnedTag && OwnedTag->isDefinition()) {
|
|
|
|
// The type-specifier-seq shall not declare a new class or enumeration.
|
|
|
|
Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition);
|
2008-09-10 06:17:11 +04:00
|
|
|
}
|
|
|
|
|
2009-06-24 01:43:56 +04:00
|
|
|
DeclPtrTy Dcl = ActOnDeclarator(S, D);
|
2008-09-10 06:17:11 +04:00
|
|
|
if (!Dcl)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2009-05-31 01:37:25 +04:00
|
|
|
AddInitializerToDecl(Dcl, move(AssignExprVal), /*DirectInit=*/false);
|
2008-09-10 06:17:11 +04:00
|
|
|
|
2008-12-11 02:01:14 +03:00
|
|
|
// Mark this variable as one that is declared within a conditional.
|
2009-03-28 22:18:32 +03:00
|
|
|
// We know that the decl had to be a VarDecl because that is the only type of
|
|
|
|
// decl that can be assigned and the grammar requires an '='.
|
|
|
|
VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>());
|
|
|
|
VD->setDeclaredInCondition(true);
|
|
|
|
return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD));
|
2008-09-10 06:17:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
|
|
|
|
bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
|
|
|
|
// C++ 6.4p4:
|
|
|
|
// The value of a condition that is an initialized declaration in a statement
|
|
|
|
// other than a switch statement is the value of the declared variable
|
|
|
|
// implicitly converted to type bool. If that conversion is ill-formed, the
|
|
|
|
// program is ill-formed.
|
|
|
|
// The value of a condition that is an expression is the value of the
|
|
|
|
// expression, implicitly converted to bool.
|
|
|
|
//
|
2009-01-14 18:45:31 +03:00
|
|
|
return PerformContextuallyConvertToBool(CondExpr);
|
2008-09-10 06:17:11 +04:00
|
|
|
}
|
2008-09-12 04:47:35 +04:00
|
|
|
|
|
|
|
/// Helper function to determine whether this is the (deprecated) C++
|
|
|
|
/// conversion from a string literal to a pointer to non-const char or
|
|
|
|
/// non-const wchar_t (for narrow and wide string literals,
|
|
|
|
/// respectively).
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
2008-09-12 04:47:35 +04:00
|
|
|
Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
|
|
|
|
// Look inside the implicit cast, if it exists.
|
|
|
|
if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
|
|
|
|
From = Cast->getSubExpr();
|
|
|
|
|
|
|
|
// A string literal (2.13.4) that is not a wide string literal can
|
|
|
|
// be converted to an rvalue of type "pointer to char"; a wide
|
|
|
|
// string literal can be converted to an rvalue of type "pointer
|
|
|
|
// to wchar_t" (C++ 4.2p2).
|
|
|
|
if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType *ToPtrType = ToType->getAs<PointerType>())
|
2009-09-09 19:08:12 +04:00
|
|
|
if (const BuiltinType *ToPointeeType
|
2009-09-22 03:43:11 +04:00
|
|
|
= ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
|
2008-09-12 04:47:35 +04:00
|
|
|
// This conversion is considered only when there is an
|
|
|
|
// explicit appropriate pointer target type (C++ 4.2p2).
|
2009-09-24 23:53:00 +04:00
|
|
|
if (!ToPtrType->getPointeeType().hasQualifiers() &&
|
2008-09-12 04:47:35 +04:00
|
|
|
((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
|
|
|
|
(!StrLit->isWide() &&
|
|
|
|
(ToPointeeType->getKind() == BuiltinType::Char_U ||
|
|
|
|
ToPointeeType->getKind() == BuiltinType::Char_S))))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
|
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType. Returns true if there was an
|
|
|
|
/// error, false otherwise. The expression From is replaced with the
|
2008-12-19 20:40:08 +03:00
|
|
|
/// converted expression. Flavor is the kind of conversion we're
|
2009-01-14 18:45:31 +03:00
|
|
|
/// performing, used in the error message. If @p AllowExplicit,
|
2009-04-12 21:16:29 +04:00
|
|
|
/// explicit user-defined conversions are permitted. @p Elidable should be true
|
|
|
|
/// when called for copies which may be elided (C++ 12.8p15). C++0x overload
|
|
|
|
/// resolution works differently in that case.
|
|
|
|
bool
|
2008-12-19 20:40:08 +03:00
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
2009-04-12 21:16:29 +04:00
|
|
|
const char *Flavor, bool AllowExplicit,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool Elidable) {
|
2009-04-12 21:16:29 +04:00
|
|
|
ImplicitConversionSequence ICS;
|
2009-09-24 00:55:32 +04:00
|
|
|
return PerformImplicitConversion(From, ToType, Flavor, AllowExplicit,
|
|
|
|
Elidable, ICS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
|
|
|
const char *Flavor, bool AllowExplicit,
|
|
|
|
bool Elidable,
|
|
|
|
ImplicitConversionSequence& ICS) {
|
2009-04-12 21:16:29 +04:00
|
|
|
ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
|
|
|
|
if (Elidable && getLangOptions().CPlusPlus0x) {
|
2009-09-09 19:08:12 +04:00
|
|
|
ICS = TryImplicitConversion(From, ToType,
|
2009-08-27 21:24:15 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
2009-09-09 19:08:12 +04:00
|
|
|
AllowExplicit,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/true,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-04-12 21:16:29 +04:00
|
|
|
}
|
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) {
|
2009-09-09 19:08:12 +04:00
|
|
|
ICS = TryImplicitConversion(From, ToType,
|
2009-08-27 21:24:15 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
AllowExplicit,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-04-12 21:16:29 +04:00
|
|
|
}
|
2009-01-14 18:45:31 +03:00
|
|
|
return PerformImplicitConversion(From, ToType, ICS, Flavor);
|
|
|
|
}
|
|
|
|
|
2009-10-16 23:20:59 +04:00
|
|
|
/// BuildCXXDerivedToBaseExpr - This routine generates the suitable AST
|
|
|
|
/// for the derived to base conversion of the expression 'From'. All
|
|
|
|
/// necessary information is passed in ICS.
|
|
|
|
bool
|
|
|
|
Sema::BuildCXXDerivedToBaseExpr(Expr *&From, CastExpr::CastKind CastKind,
|
|
|
|
const ImplicitConversionSequence& ICS,
|
|
|
|
const char *Flavor) {
|
|
|
|
QualType BaseType =
|
|
|
|
QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr);
|
|
|
|
// Must do additional defined to base conversion.
|
|
|
|
QualType DerivedType =
|
|
|
|
QualType::getFromOpaquePtr(ICS.UserDefined.After.FromTypePtr);
|
|
|
|
|
|
|
|
From = new (Context) ImplicitCastExpr(
|
|
|
|
DerivedType.getNonReferenceType(),
|
|
|
|
CastKind,
|
|
|
|
From,
|
|
|
|
DerivedType->isLValueReferenceType());
|
|
|
|
From = new (Context) ImplicitCastExpr(BaseType.getNonReferenceType(),
|
|
|
|
CastExpr::CK_DerivedToBase, From,
|
|
|
|
BaseType->isLValueReferenceType());
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
OwningExprResult FromResult =
|
|
|
|
BuildCXXConstructExpr(
|
|
|
|
ICS.UserDefined.After.CopyConstructor->getLocation(),
|
|
|
|
BaseType,
|
|
|
|
ICS.UserDefined.After.CopyConstructor,
|
|
|
|
MultiExprArg(*this, (void **)&From, 1));
|
|
|
|
if (FromResult.isInvalid())
|
|
|
|
return true;
|
|
|
|
From = FromResult.takeAs<Expr>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-14 18:45:31 +03:00
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType using the pre-computed implicit
|
|
|
|
/// conversion sequence ICS. Returns true if there was an error, false
|
|
|
|
/// otherwise. The expression From is replaced with the converted
|
|
|
|
/// expression. Flavor is the kind of conversion we're performing,
|
|
|
|
/// used in the error message.
|
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
|
|
|
const ImplicitConversionSequence &ICS,
|
|
|
|
const char* Flavor) {
|
2008-10-24 08:54:22 +04:00
|
|
|
switch (ICS.ConversionKind) {
|
|
|
|
case ImplicitConversionSequence::StandardConversion:
|
2008-12-19 20:40:08 +03:00
|
|
|
if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
|
2008-10-24 08:54:22 +04:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
2009-09-15 10:28:28 +04:00
|
|
|
case ImplicitConversionSequence::UserDefinedConversion: {
|
|
|
|
|
2009-08-29 02:04:50 +04:00
|
|
|
FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
|
|
|
|
CastExpr::CastKind CastKind = CastExpr::CK_Unknown;
|
2009-09-15 10:28:28 +04:00
|
|
|
QualType BeforeToType;
|
|
|
|
if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) {
|
2009-08-29 02:04:50 +04:00
|
|
|
CastKind = CastExpr::CK_UserDefinedConversion;
|
2009-09-15 10:28:28 +04:00
|
|
|
|
|
|
|
// If the user-defined conversion is specified by a conversion function,
|
|
|
|
// the initial standard conversion sequence converts the source type to
|
|
|
|
// the implicit object parameter of the conversion function.
|
|
|
|
BeforeToType = Context.getTagDeclType(Conv->getParent());
|
|
|
|
} else if (const CXXConstructorDecl *Ctor =
|
|
|
|
dyn_cast<CXXConstructorDecl>(FD)) {
|
2009-09-10 01:33:21 +04:00
|
|
|
CastKind = CastExpr::CK_ConstructorConversion;
|
2009-09-15 10:28:28 +04:00
|
|
|
|
|
|
|
// If the user-defined conversion is specified by a constructor, the
|
|
|
|
// initial standard conversion sequence converts the source type to the
|
|
|
|
// type required by the argument of the constructor
|
|
|
|
BeforeToType = Ctor->getParamDecl(0)->getType();
|
|
|
|
}
|
2009-09-10 01:33:21 +04:00
|
|
|
else
|
|
|
|
assert(0 && "Unknown conversion function kind!");
|
|
|
|
|
2009-09-15 10:28:28 +04:00
|
|
|
if (PerformImplicitConversion(From, BeforeToType,
|
|
|
|
ICS.UserDefined.Before, "converting"))
|
|
|
|
return true;
|
|
|
|
|
2009-09-10 01:33:21 +04:00
|
|
|
OwningExprResult CastArg
|
|
|
|
= BuildCXXCastArgument(From->getLocStart(),
|
|
|
|
ToType.getNonReferenceType(),
|
|
|
|
CastKind, cast<CXXMethodDecl>(FD),
|
|
|
|
Owned(From));
|
|
|
|
|
|
|
|
if (CastArg.isInvalid())
|
|
|
|
return true;
|
2009-10-16 23:20:59 +04:00
|
|
|
|
|
|
|
if (ICS.UserDefined.After.Second == ICK_Derived_To_Base &&
|
|
|
|
ICS.UserDefined.After.CopyConstructor) {
|
|
|
|
From = CastArg.takeAs<Expr>();
|
|
|
|
return BuildCXXDerivedToBaseExpr(From, CastKind, ICS, Flavor);
|
|
|
|
}
|
2009-10-23 22:08:22 +04:00
|
|
|
|
|
|
|
if (ICS.UserDefined.After.Second == ICK_Pointer_Member &&
|
|
|
|
ToType.getNonReferenceType()->isMemberFunctionPointerType())
|
|
|
|
CastKind = CastExpr::CK_BaseToDerivedMemberPointer;
|
2009-09-10 01:33:21 +04:00
|
|
|
|
2009-09-15 09:49:31 +04:00
|
|
|
From = new (Context) ImplicitCastExpr(ToType.getNonReferenceType(),
|
2009-10-16 23:20:59 +04:00
|
|
|
CastKind, CastArg.takeAs<Expr>(),
|
2009-09-15 09:49:31 +04:00
|
|
|
ToType->isLValueReferenceType());
|
2009-08-29 02:04:50 +04:00
|
|
|
return false;
|
2009-10-16 23:20:59 +04:00
|
|
|
}
|
|
|
|
|
2008-10-24 08:54:22 +04:00
|
|
|
case ImplicitConversionSequence::EllipsisConversion:
|
|
|
|
assert(false && "Cannot perform an ellipsis conversion");
|
2008-10-31 19:23:19 +03:00
|
|
|
return false;
|
2008-10-24 08:54:22 +04:00
|
|
|
|
|
|
|
case ImplicitConversionSequence::BadConversion:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything went well.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType by following the standard
|
|
|
|
/// conversion sequence SCS. Returns true if there was an error, false
|
|
|
|
/// otherwise. The expression From is replaced with the converted
|
2008-12-19 20:40:08 +03:00
|
|
|
/// expression. Flavor is the context in which we're performing this
|
|
|
|
/// conversion, for use in error messages.
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
2008-10-24 08:54:22 +04:00
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
2008-12-19 20:40:08 +03:00
|
|
|
const StandardConversionSequence& SCS,
|
2009-01-14 18:45:31 +03:00
|
|
|
const char *Flavor) {
|
2009-05-16 11:39:55 +04:00
|
|
|
// Overall FIXME: we are recomputing too many types here and doing far too
|
|
|
|
// much extra work. What this means is that we need to keep track of more
|
|
|
|
// information that is computed when we try the implicit conversion initially,
|
|
|
|
// so that we don't need to recompute anything here.
|
2008-10-24 08:54:22 +04:00
|
|
|
QualType FromType = From->getType();
|
|
|
|
|
2008-11-03 22:09:14 +03:00
|
|
|
if (SCS.CopyConstructor) {
|
2009-05-19 08:45:15 +04:00
|
|
|
// FIXME: When can ToType be a reference type?
|
|
|
|
assert(!ToType->isReferenceType());
|
2009-09-25 22:59:21 +04:00
|
|
|
if (SCS.Second == ICK_Derived_To_Base) {
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor),
|
|
|
|
MultiExprArg(*this, (void **)&From, 1),
|
|
|
|
/*FIXME:ConstructLoc*/SourceLocation(),
|
|
|
|
ConstructorArgs))
|
|
|
|
return true;
|
|
|
|
OwningExprResult FromResult =
|
|
|
|
BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
|
|
|
|
ToType, SCS.CopyConstructor,
|
|
|
|
move_arg(ConstructorArgs));
|
|
|
|
if (FromResult.isInvalid())
|
|
|
|
return true;
|
|
|
|
From = FromResult.takeAs<Expr>();
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
OwningExprResult FromResult =
|
|
|
|
BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
|
|
|
|
ToType, SCS.CopyConstructor,
|
2009-09-08 02:23:31 +04:00
|
|
|
MultiExprArg(*this, (void**)&From, 1));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-25 09:12:04 +04:00
|
|
|
if (FromResult.isInvalid())
|
|
|
|
return true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-25 09:12:04 +04:00
|
|
|
From = FromResult.takeAs<Expr>();
|
2008-11-03 22:09:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-24 08:54:22 +04:00
|
|
|
// Perform the first implicit conversion.
|
|
|
|
switch (SCS.First) {
|
|
|
|
case ICK_Identity:
|
|
|
|
case ICK_Lvalue_To_Rvalue:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Array_To_Pointer:
|
2009-02-19 00:56:37 +03:00
|
|
|
FromType = Context.getArrayDecayedType(FromType);
|
2009-08-09 01:04:35 +04:00
|
|
|
ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay);
|
2009-02-19 00:56:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Function_To_Pointer:
|
2009-03-13 21:40:31 +03:00
|
|
|
if (Context.getCanonicalType(FromType) == Context.OverloadTy) {
|
2008-11-10 23:40:00 +03:00
|
|
|
FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
|
|
|
|
if (!Fn)
|
|
|
|
return true;
|
|
|
|
|
2009-02-19 00:56:37 +03:00
|
|
|
if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin()))
|
|
|
|
return true;
|
|
|
|
|
2009-10-21 21:16:23 +04:00
|
|
|
From = FixOverloadedFunctionReference(From, Fn);
|
2008-11-10 23:40:00 +03:00
|
|
|
FromType = From->getType();
|
2009-10-21 21:16:23 +04:00
|
|
|
|
2009-10-18 00:50:27 +04:00
|
|
|
// If there's already an address-of operator in the expression, we have
|
|
|
|
// the right type already, and the code below would just introduce an
|
|
|
|
// invalid additional pointer level.
|
2009-10-21 21:16:23 +04:00
|
|
|
if (FromType->isPointerType() || FromType->isMemberFunctionPointerType())
|
2009-10-18 00:50:27 +04:00
|
|
|
break;
|
2008-11-10 23:40:00 +03:00
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
FromType = Context.getPointerType(FromType);
|
2009-09-02 00:37:18 +04:00
|
|
|
ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay);
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper first standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the second implicit conversion
|
|
|
|
switch (SCS.Second) {
|
|
|
|
case ICK_Identity:
|
2009-10-10 16:04:10 +04:00
|
|
|
// If both sides are functions (or pointers/references to them), there could
|
|
|
|
// be incompatible exception declarations.
|
|
|
|
if (CheckExceptionSpecCompatibility(From, ToType))
|
|
|
|
return true;
|
|
|
|
// Nothing else to do.
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Integral_Promotion:
|
|
|
|
case ICK_Integral_Conversion:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Floating_Promotion:
|
2008-10-24 08:54:22 +04:00
|
|
|
case ICK_Floating_Conversion:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Complex_Promotion:
|
2009-02-12 03:15:05 +03:00
|
|
|
case ICK_Complex_Conversion:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_Unknown);
|
|
|
|
break;
|
|
|
|
|
2008-10-24 08:54:22 +04:00
|
|
|
case ICK_Floating_Integral:
|
2009-10-20 12:27:19 +04:00
|
|
|
if (ToType->isFloatingType())
|
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating);
|
|
|
|
else
|
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral);
|
|
|
|
break;
|
|
|
|
|
2009-02-12 03:15:05 +03:00
|
|
|
case ICK_Complex_Real:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_Unknown);
|
|
|
|
break;
|
|
|
|
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
case ICK_Compatible_Conversion:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, ToType, CastExpr::CK_NoOp);
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
2009-09-12 08:46:44 +04:00
|
|
|
case ICK_Pointer_Conversion: {
|
2008-12-19 20:40:08 +03:00
|
|
|
if (SCS.IncompatibleObjC) {
|
|
|
|
// Diagnose incompatible Objective-C conversions
|
2009-09-09 19:08:12 +04:00
|
|
|
Diag(From->getSourceRange().getBegin(),
|
2008-12-19 20:40:08 +03:00
|
|
|
diag::ext_typecheck_convert_incompatible_pointer)
|
|
|
|
<< From->getType() << ToType << Flavor
|
|
|
|
<< From->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2009-09-12 08:46:44 +04:00
|
|
|
|
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
|
|
|
if (CheckPointerConversion(From, ToType, Kind))
|
2008-10-24 08:54:22 +04:00
|
|
|
return true;
|
2009-09-12 08:46:44 +04:00
|
|
|
ImpCastExprToType(From, ToType, Kind);
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
2009-09-12 08:46:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case ICK_Pointer_Member: {
|
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
|
|
|
if (CheckMemberPointerConversion(From, ToType, Kind))
|
|
|
|
return true;
|
2009-10-10 16:04:10 +04:00
|
|
|
if (CheckExceptionSpecCompatibility(From, ToType))
|
|
|
|
return true;
|
2009-09-12 08:46:44 +04:00
|
|
|
ImpCastExprToType(From, ToType, Kind);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
case ICK_Boolean_Conversion:
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(From, Context.BoolTy, CastExpr::CK_Unknown);
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SCS.Third) {
|
|
|
|
case ICK_Identity:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Qualification:
|
2009-05-16 11:39:55 +04:00
|
|
|
// FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue
|
|
|
|
// references.
|
2009-09-09 19:08:12 +04:00
|
|
|
ImpCastExprToType(From, ToType.getNonReferenceType(),
|
2009-10-20 12:27:19 +04:00
|
|
|
CastExpr::CK_NoOp,
|
2009-03-17 02:22:08 +03:00
|
|
|
ToType->isLValueReferenceType());
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:52:13 +03:00
|
|
|
Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
|
|
|
|
SourceLocation KWLoc,
|
|
|
|
SourceLocation LParen,
|
|
|
|
TypeTy *Ty,
|
|
|
|
SourceLocation RParen) {
|
2009-08-19 05:28:28 +04:00
|
|
|
QualType T = GetTypeFromParser(Ty);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-07 23:06:02 +04:00
|
|
|
// According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
|
|
|
|
// all traits except __is_class, __is_enum and __is_union require a the type
|
|
|
|
// to be complete.
|
|
|
|
if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (RequireCompleteType(KWLoc, T,
|
2009-08-27 02:59:12 +04:00
|
|
|
diag::err_incomplete_type_used_in_type_trait_expr))
|
2009-07-07 23:06:02 +04:00
|
|
|
return ExprError();
|
|
|
|
}
|
2009-01-05 23:52:13 +03:00
|
|
|
|
|
|
|
// There is no point in eagerly computing the value. The traits are designed
|
|
|
|
// to be used from type trait templates, so Ty will be a template parameter
|
|
|
|
// 99% of the time.
|
2009-07-07 23:06:02 +04:00
|
|
|
return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T,
|
|
|
|
RParen, Context.BoolTy));
|
2009-01-05 23:52:13 +03:00
|
|
|
}
|
2009-02-07 23:10:22 +03:00
|
|
|
|
|
|
|
QualType Sema::CheckPointerToMemberOperands(
|
2009-09-09 19:08:12 +04:00
|
|
|
Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) {
|
2009-02-07 23:10:22 +03:00
|
|
|
const char *OpSpelling = isIndirect ? "->*" : ".*";
|
|
|
|
// C++ 5.5p2
|
|
|
|
// The binary operator .* [p3: ->*] binds its second operand, which shall
|
|
|
|
// be of type "pointer to member of T" (where T is a completely-defined
|
|
|
|
// class type) [...]
|
|
|
|
QualType RType = rex->getType();
|
2009-07-30 01:53:49 +04:00
|
|
|
const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>();
|
2009-03-24 22:52:54 +03:00
|
|
|
if (!MemPtr) {
|
2009-02-07 23:10:22 +03:00
|
|
|
Diag(Loc, diag::err_bad_memptr_rhs)
|
|
|
|
<< OpSpelling << RType << rex->getSourceRange();
|
|
|
|
return QualType();
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-03-24 22:52:54 +03:00
|
|
|
|
2009-02-07 23:10:22 +03:00
|
|
|
QualType Class(MemPtr->getClass(), 0);
|
|
|
|
|
|
|
|
// C++ 5.5p2
|
|
|
|
// [...] to its first operand, which shall be of class T or of a class of
|
|
|
|
// which T is an unambiguous and accessible base class. [p3: a pointer to
|
|
|
|
// such a class]
|
|
|
|
QualType LType = lex->getType();
|
|
|
|
if (isIndirect) {
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType *Ptr = LType->getAs<PointerType>())
|
2009-02-07 23:10:22 +03:00
|
|
|
LType = Ptr->getPointeeType().getNonReferenceType();
|
|
|
|
else {
|
|
|
|
Diag(Loc, diag::err_bad_memptr_lhs)
|
2009-10-26 23:45:27 +03:00
|
|
|
<< OpSpelling << 1 << LType
|
|
|
|
<< CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*");
|
2009-02-07 23:10:22 +03:00
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Context.getCanonicalType(Class).getUnqualifiedType() !=
|
|
|
|
Context.getCanonicalType(LType).getUnqualifiedType()) {
|
2009-10-06 21:59:45 +04:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
|
|
|
|
/*DetectVirtual=*/false);
|
2009-05-16 11:39:55 +04:00
|
|
|
// FIXME: Would it be useful to print full ambiguity paths, or is that
|
|
|
|
// overkill?
|
2009-02-07 23:10:22 +03:00
|
|
|
if (!IsDerivedFrom(LType, Class, Paths) ||
|
|
|
|
Paths.isAmbiguous(Context.getCanonicalType(Class))) {
|
2009-10-26 23:45:27 +03:00
|
|
|
const char *ReplaceStr = isIndirect ? ".*" : "->*";
|
2009-02-07 23:10:22 +03:00
|
|
|
Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
|
2009-10-26 23:45:27 +03:00
|
|
|
<< (int)isIndirect << lex->getType() <<
|
|
|
|
CodeModificationHint::CreateReplacement(SourceRange(Loc), ReplaceStr);
|
2009-02-07 23:10:22 +03:00
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ 5.5p2
|
|
|
|
// The result is an object or a function of the type specified by the
|
|
|
|
// second operand.
|
|
|
|
// The cv qualifiers are the union of those in the pointer and the left side,
|
|
|
|
// in accordance with 5.5p5 and 5.2.5.
|
|
|
|
// FIXME: This returns a dereferenced member function pointer as a normal
|
|
|
|
// function type. However, the only operation valid on such functions is
|
2009-05-16 11:39:55 +04:00
|
|
|
// calling them. There's also a GCC extension to get a function pointer to the
|
|
|
|
// thing, which is another complication, because this type - unlike the type
|
|
|
|
// that is the result of this expression - takes the class as the first
|
2009-02-07 23:10:22 +03:00
|
|
|
// argument.
|
|
|
|
// We probably need a "MemberFunctionClosureType" or something like that.
|
|
|
|
QualType Result = MemPtr->getPointeeType();
|
2009-09-24 23:53:00 +04:00
|
|
|
Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers());
|
2009-02-07 23:10:22 +03:00
|
|
|
return Result;
|
|
|
|
}
|
2009-04-16 21:51:27 +04:00
|
|
|
|
|
|
|
/// \brief Get the target type of a standard or user-defined conversion.
|
|
|
|
static QualType TargetType(const ImplicitConversionSequence &ICS) {
|
|
|
|
assert((ICS.ConversionKind ==
|
|
|
|
ImplicitConversionSequence::StandardConversion ||
|
|
|
|
ICS.ConversionKind ==
|
|
|
|
ImplicitConversionSequence::UserDefinedConversion) &&
|
|
|
|
"function only valid for standard or user-defined conversions");
|
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion)
|
|
|
|
return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr);
|
|
|
|
return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Try to convert a type to another according to C++0x 5.16p3.
|
|
|
|
///
|
|
|
|
/// This is part of the parameter validation for the ? operator. If either
|
|
|
|
/// value operand is a class type, the two operands are attempted to be
|
|
|
|
/// converted to each other. This function does the conversion in one direction.
|
|
|
|
/// It emits a diagnostic and returns true only if it finds an ambiguous
|
|
|
|
/// conversion.
|
|
|
|
static bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
|
|
|
|
SourceLocation QuestionLoc,
|
2009-09-09 19:08:12 +04:00
|
|
|
ImplicitConversionSequence &ICS) {
|
2009-04-16 21:51:27 +04:00
|
|
|
// C++0x 5.16p3
|
|
|
|
// The process for determining whether an operand expression E1 of type T1
|
|
|
|
// can be converted to match an operand expression E2 of type T2 is defined
|
|
|
|
// as follows:
|
|
|
|
// -- If E2 is an lvalue:
|
|
|
|
if (To->isLvalue(Self.Context) == Expr::LV_Valid) {
|
|
|
|
// E1 can be converted to match E2 if E1 can be implicitly converted to
|
|
|
|
// type "lvalue reference to T2", subject to the constraint that in the
|
|
|
|
// conversion the reference must bind directly to E1.
|
|
|
|
if (!Self.CheckReferenceInit(From,
|
|
|
|
Self.Context.getLValueReferenceType(To->getType()),
|
2009-09-24 03:04:10 +04:00
|
|
|
To->getLocStart(),
|
2009-08-27 21:30:43 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
|
|
|
/*ForceRValue=*/false,
|
|
|
|
&ICS))
|
2009-04-16 21:51:27 +04:00
|
|
|
{
|
|
|
|
assert((ICS.ConversionKind ==
|
|
|
|
ImplicitConversionSequence::StandardConversion ||
|
|
|
|
ICS.ConversionKind ==
|
|
|
|
ImplicitConversionSequence::UserDefinedConversion) &&
|
|
|
|
"expected a definite conversion");
|
|
|
|
bool DirectBinding =
|
|
|
|
ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ?
|
|
|
|
ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding;
|
|
|
|
if (DirectBinding)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
|
|
|
|
// -- If E2 is an rvalue, or if the conversion above cannot be done:
|
|
|
|
// -- if E1 and E2 have class type, and the underlying class types are
|
|
|
|
// the same or one is a base class of the other:
|
|
|
|
QualType FTy = From->getType();
|
|
|
|
QualType TTy = To->getType();
|
2009-07-30 01:53:49 +04:00
|
|
|
const RecordType *FRec = FTy->getAs<RecordType>();
|
|
|
|
const RecordType *TRec = TTy->getAs<RecordType>();
|
2009-04-16 21:51:27 +04:00
|
|
|
bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy);
|
|
|
|
if (FRec && TRec && (FRec == TRec ||
|
|
|
|
FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) {
|
|
|
|
// E1 can be converted to match E2 if the class of T2 is the
|
|
|
|
// same type as, or a base class of, the class of T1, and
|
|
|
|
// [cv2 > cv1].
|
|
|
|
if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) {
|
|
|
|
// Could still fail if there's no copy constructor.
|
|
|
|
// FIXME: Is this a hard error then, or just a conversion failure? The
|
|
|
|
// standard doesn't say.
|
2009-09-09 19:08:12 +04:00
|
|
|
ICS = Self.TryCopyInitialization(From, TTy,
|
2009-08-27 21:18:13 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
2009-08-27 21:37:39 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-04-16 21:51:27 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// -- Otherwise: E1 can be converted to match E2 if E1 can be
|
|
|
|
// implicitly converted to the type that expression E2 would have
|
|
|
|
// if E2 were converted to an rvalue.
|
|
|
|
// First find the decayed type.
|
|
|
|
if (TTy->isFunctionType())
|
|
|
|
TTy = Self.Context.getPointerType(TTy);
|
2009-09-09 19:08:12 +04:00
|
|
|
else if (TTy->isArrayType())
|
2009-04-16 21:51:27 +04:00
|
|
|
TTy = Self.Context.getArrayDecayedType(TTy);
|
|
|
|
|
|
|
|
// Now try the implicit conversion.
|
|
|
|
// FIXME: This doesn't detect ambiguities.
|
2009-08-27 21:24:15 +04:00
|
|
|
ICS = Self.TryImplicitConversion(From, TTy,
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-04-16 21:51:27 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Try to find a common type for two according to C++0x 5.16p5.
|
|
|
|
///
|
|
|
|
/// This is part of the parameter validation for the ? operator. If either
|
|
|
|
/// value operand is a class type, overload resolution is used to find a
|
|
|
|
/// conversion to a common type.
|
|
|
|
static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
Expr *Args[2] = { LHS, RHS };
|
|
|
|
OverloadCandidateSet CandidateSet;
|
2009-10-22 03:19:44 +04:00
|
|
|
Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet);
|
2009-04-16 21:51:27 +04:00
|
|
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
2009-06-20 03:52:42 +04:00
|
|
|
switch (Self.BestViableFunction(CandidateSet, Loc, Best)) {
|
2009-04-16 21:51:27 +04:00
|
|
|
case Sema::OR_Success:
|
|
|
|
// We found a match. Perform the conversions on the arguments and move on.
|
|
|
|
if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0],
|
|
|
|
Best->Conversions[0], "converting") ||
|
|
|
|
Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1],
|
|
|
|
Best->Conversions[1], "converting"))
|
|
|
|
break;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Sema::OR_No_Viable_Function:
|
|
|
|
Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands)
|
|
|
|
<< LHS->getType() << RHS->getType()
|
|
|
|
<< LHS->getSourceRange() << RHS->getSourceRange();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Sema::OR_Ambiguous:
|
|
|
|
Self.Diag(Loc, diag::err_conditional_ambiguous_ovl)
|
|
|
|
<< LHS->getType() << RHS->getType()
|
|
|
|
<< LHS->getSourceRange() << RHS->getSourceRange();
|
2009-05-16 11:39:55 +04:00
|
|
|
// FIXME: Print the possible common types by printing the return types of
|
|
|
|
// the viable candidates.
|
2009-04-16 21:51:27 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Sema::OR_Deleted:
|
|
|
|
assert(false && "Conditional operator has only built-in overloads");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-17 20:30:52 +04:00
|
|
|
/// \brief Perform an "extended" implicit conversion as returned by
|
|
|
|
/// TryClassUnification.
|
|
|
|
///
|
|
|
|
/// TryClassUnification generates ICSs that include reference bindings.
|
|
|
|
/// PerformImplicitConversion is not suitable for this; it chokes if the
|
|
|
|
/// second part of a standard conversion is ICK_DerivedToBase. This function
|
|
|
|
/// handles the reference binding specially.
|
|
|
|
static bool ConvertForConditional(Sema &Self, Expr *&E,
|
2009-09-09 19:08:12 +04:00
|
|
|
const ImplicitConversionSequence &ICS) {
|
2009-04-17 20:30:52 +04:00
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion &&
|
|
|
|
ICS.Standard.ReferenceBinding) {
|
|
|
|
assert(ICS.Standard.DirectBinding &&
|
|
|
|
"TryClassUnification should never generate indirect ref bindings");
|
2009-04-26 15:21:02 +04:00
|
|
|
// FIXME: CheckReferenceInit should be able to reuse the ICS instead of
|
|
|
|
// redoing all the work.
|
|
|
|
return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
|
2009-08-27 21:30:43 +04:00
|
|
|
TargetType(ICS)),
|
2009-09-24 03:04:10 +04:00
|
|
|
/*FIXME:*/E->getLocStart(),
|
2009-08-27 21:30:43 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
|
|
|
/*ForceRValue=*/false);
|
2009-04-17 20:30:52 +04:00
|
|
|
}
|
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion &&
|
|
|
|
ICS.UserDefined.After.ReferenceBinding) {
|
|
|
|
assert(ICS.UserDefined.After.DirectBinding &&
|
|
|
|
"TryClassUnification should never generate indirect ref bindings");
|
2009-04-26 15:21:02 +04:00
|
|
|
return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
|
2009-08-27 21:30:43 +04:00
|
|
|
TargetType(ICS)),
|
2009-09-24 03:04:10 +04:00
|
|
|
/*FIXME:*/E->getLocStart(),
|
2009-08-27 21:30:43 +04:00
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
|
|
|
/*ForceRValue=*/false);
|
2009-04-17 20:30:52 +04:00
|
|
|
}
|
|
|
|
if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting"))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-16 21:51:27 +04:00
|
|
|
/// \brief Check the operands of ?: under C++ semantics.
|
|
|
|
///
|
|
|
|
/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y
|
|
|
|
/// extension. In this case, LHS == Cond. (But they're not aliases.)
|
|
|
|
QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS,
|
|
|
|
SourceLocation QuestionLoc) {
|
2009-05-16 11:39:55 +04:00
|
|
|
// FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++
|
|
|
|
// interface pointers.
|
2009-04-16 21:51:27 +04:00
|
|
|
|
|
|
|
// C++0x 5.16p1
|
|
|
|
// The first expression is contextually converted to bool.
|
|
|
|
if (!Cond->isTypeDependent()) {
|
|
|
|
if (CheckCXXBooleanCondition(Cond))
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either of the arguments dependent?
|
|
|
|
if (LHS->isTypeDependent() || RHS->isTypeDependent())
|
|
|
|
return Context.DependentTy;
|
|
|
|
|
|
|
|
// C++0x 5.16p2
|
|
|
|
// If either the second or the third operand has type (cv) void, ...
|
|
|
|
QualType LTy = LHS->getType();
|
|
|
|
QualType RTy = RHS->getType();
|
|
|
|
bool LVoid = LTy->isVoidType();
|
|
|
|
bool RVoid = RTy->isVoidType();
|
|
|
|
if (LVoid || RVoid) {
|
|
|
|
// ... then the [l2r] conversions are performed on the second and third
|
|
|
|
// operands ...
|
|
|
|
DefaultFunctionArrayConversion(LHS);
|
|
|
|
DefaultFunctionArrayConversion(RHS);
|
|
|
|
LTy = LHS->getType();
|
|
|
|
RTy = RHS->getType();
|
|
|
|
|
|
|
|
// ... and one of the following shall hold:
|
|
|
|
// -- The second or the third operand (but not both) is a throw-
|
|
|
|
// expression; the result is of the type of the other and is an rvalue.
|
|
|
|
bool LThrow = isa<CXXThrowExpr>(LHS);
|
|
|
|
bool RThrow = isa<CXXThrowExpr>(RHS);
|
|
|
|
if (LThrow && !RThrow)
|
|
|
|
return RTy;
|
|
|
|
if (RThrow && !LThrow)
|
|
|
|
return LTy;
|
|
|
|
|
|
|
|
// -- Both the second and third operands have type void; the result is of
|
|
|
|
// type void and is an rvalue.
|
|
|
|
if (LVoid && RVoid)
|
|
|
|
return Context.VoidTy;
|
|
|
|
|
|
|
|
// Neither holds, error.
|
|
|
|
Diag(QuestionLoc, diag::err_conditional_void_nonvoid)
|
|
|
|
<< (LVoid ? RTy : LTy) << (LVoid ? 0 : 1)
|
|
|
|
<< LHS->getSourceRange() << RHS->getSourceRange();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither is void.
|
|
|
|
|
|
|
|
// C++0x 5.16p3
|
|
|
|
// Otherwise, if the second and third operand have different types, and
|
|
|
|
// either has (cv) class type, and attempt is made to convert each of those
|
|
|
|
// operands to the other.
|
|
|
|
if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) &&
|
|
|
|
(LTy->isRecordType() || RTy->isRecordType())) {
|
|
|
|
ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft;
|
|
|
|
// These return true if a single direction is already ambiguous.
|
|
|
|
if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight))
|
|
|
|
return QualType();
|
|
|
|
if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft))
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
bool HaveL2R = ICSLeftToRight.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion;
|
|
|
|
bool HaveR2L = ICSRightToLeft.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion;
|
|
|
|
// If both can be converted, [...] the program is ill-formed.
|
|
|
|
if (HaveL2R && HaveR2L) {
|
|
|
|
Diag(QuestionLoc, diag::err_conditional_ambiguous)
|
|
|
|
<< LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If exactly one conversion is possible, that conversion is applied to
|
|
|
|
// the chosen operand and the converted operands are used in place of the
|
|
|
|
// original operands for the remainder of this section.
|
|
|
|
if (HaveL2R) {
|
2009-04-17 20:30:52 +04:00
|
|
|
if (ConvertForConditional(*this, LHS, ICSLeftToRight))
|
2009-04-16 21:51:27 +04:00
|
|
|
return QualType();
|
|
|
|
LTy = LHS->getType();
|
|
|
|
} else if (HaveR2L) {
|
2009-04-17 20:30:52 +04:00
|
|
|
if (ConvertForConditional(*this, RHS, ICSRightToLeft))
|
2009-04-16 21:51:27 +04:00
|
|
|
return QualType();
|
|
|
|
RTy = RHS->getType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++0x 5.16p4
|
|
|
|
// If the second and third operands are lvalues and have the same type,
|
|
|
|
// the result is of that type [...]
|
|
|
|
bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy);
|
|
|
|
if (Same && LHS->isLvalue(Context) == Expr::LV_Valid &&
|
|
|
|
RHS->isLvalue(Context) == Expr::LV_Valid)
|
|
|
|
return LTy;
|
|
|
|
|
|
|
|
// C++0x 5.16p5
|
|
|
|
// Otherwise, the result is an rvalue. If the second and third operands
|
|
|
|
// do not have the same type, and either has (cv) class type, ...
|
|
|
|
if (!Same && (LTy->isRecordType() || RTy->isRecordType())) {
|
|
|
|
// ... overload resolution is used to determine the conversions (if any)
|
|
|
|
// to be applied to the operands. If the overload resolution fails, the
|
|
|
|
// program is ill-formed.
|
|
|
|
if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc))
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++0x 5.16p6
|
|
|
|
// LValue-to-rvalue, array-to-pointer, and function-to-pointer standard
|
|
|
|
// conversions are performed on the second and third operands.
|
|
|
|
DefaultFunctionArrayConversion(LHS);
|
|
|
|
DefaultFunctionArrayConversion(RHS);
|
|
|
|
LTy = LHS->getType();
|
|
|
|
RTy = RHS->getType();
|
|
|
|
|
|
|
|
// After those conversions, one of the following shall hold:
|
|
|
|
// -- The second and third operands have the same type; the result
|
|
|
|
// is of that type.
|
|
|
|
if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy))
|
|
|
|
return LTy;
|
|
|
|
|
|
|
|
// -- The second and third operands have arithmetic or enumeration type;
|
|
|
|
// the usual arithmetic conversions are performed to bring them to a
|
|
|
|
// common type, and the result is of that type.
|
|
|
|
if (LTy->isArithmeticType() && RTy->isArithmeticType()) {
|
|
|
|
UsualArithmeticConversions(LHS, RHS);
|
|
|
|
return LHS->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- The second and third operands have pointer type, or one has pointer
|
|
|
|
// type and the other is a null pointer constant; pointer conversions
|
|
|
|
// and qualification conversions are performed to bring them to their
|
|
|
|
// composite pointer type. The result is of the composite pointer type.
|
2009-04-19 23:26:31 +04:00
|
|
|
QualType Composite = FindCompositePointerType(LHS, RHS);
|
|
|
|
if (!Composite.isNull())
|
|
|
|
return Composite;
|
2009-04-16 21:51:27 +04:00
|
|
|
|
2009-04-20 01:15:26 +04:00
|
|
|
// Fourth bullet is same for pointers-to-member. However, the possible
|
|
|
|
// conversions are far more limited: we have null-to-pointer, upcast of
|
|
|
|
// containing class, and second-level cv-ness.
|
|
|
|
// cv-ness is not a union, but must match one of the two operands. (Which,
|
|
|
|
// frankly, is stupid.)
|
2009-07-30 01:53:49 +04:00
|
|
|
const MemberPointerType *LMemPtr = LTy->getAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *RMemPtr = RTy->getAs<MemberPointerType>();
|
2009-09-25 08:25:58 +04:00
|
|
|
if (LMemPtr &&
|
|
|
|
RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(RHS, LTy, CastExpr::CK_NullToMemberPointer);
|
2009-04-20 01:15:26 +04:00
|
|
|
return LTy;
|
|
|
|
}
|
2009-09-25 08:25:58 +04:00
|
|
|
if (RMemPtr &&
|
|
|
|
LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
|
2009-10-20 12:27:19 +04:00
|
|
|
ImpCastExprToType(LHS, RTy, CastExpr::CK_NullToMemberPointer);
|
2009-04-20 01:15:26 +04:00
|
|
|
return RTy;
|
|
|
|
}
|
|
|
|
if (LMemPtr && RMemPtr) {
|
|
|
|
QualType LPointee = LMemPtr->getPointeeType();
|
|
|
|
QualType RPointee = RMemPtr->getPointeeType();
|
2009-09-24 23:53:00 +04:00
|
|
|
|
|
|
|
QualifierCollector LPQuals, RPQuals;
|
|
|
|
const Type *LPCan = LPQuals.strip(Context.getCanonicalType(LPointee));
|
|
|
|
const Type *RPCan = RPQuals.strip(Context.getCanonicalType(RPointee));
|
|
|
|
|
2009-04-20 01:15:26 +04:00
|
|
|
// First, we check that the unqualified pointee type is the same. If it's
|
|
|
|
// not, there's no conversion that will unify the two pointers.
|
2009-09-24 23:53:00 +04:00
|
|
|
if (LPCan == RPCan) {
|
|
|
|
|
|
|
|
// Second, we take the greater of the two qualifications. If neither
|
2009-04-20 01:15:26 +04:00
|
|
|
// is greater than the other, the conversion is not possible.
|
2009-09-24 23:53:00 +04:00
|
|
|
|
|
|
|
Qualifiers MergedQuals = LPQuals + RPQuals;
|
|
|
|
|
|
|
|
bool CompatibleQuals = true;
|
|
|
|
if (MergedQuals.getCVRQualifiers() != LPQuals.getCVRQualifiers() &&
|
|
|
|
MergedQuals.getCVRQualifiers() != RPQuals.getCVRQualifiers())
|
|
|
|
CompatibleQuals = false;
|
|
|
|
else if (LPQuals.getAddressSpace() != RPQuals.getAddressSpace())
|
|
|
|
// FIXME:
|
|
|
|
// C99 6.5.15 as modified by TR 18037:
|
|
|
|
// If the second and third operands are pointers into different
|
|
|
|
// address spaces, the address spaces must overlap.
|
|
|
|
CompatibleQuals = false;
|
|
|
|
// FIXME: GC qualifiers?
|
|
|
|
|
|
|
|
if (CompatibleQuals) {
|
2009-04-20 01:15:26 +04:00
|
|
|
// Third, we check if either of the container classes is derived from
|
|
|
|
// the other.
|
|
|
|
QualType LContainer(LMemPtr->getClass(), 0);
|
|
|
|
QualType RContainer(RMemPtr->getClass(), 0);
|
|
|
|
QualType MoreDerived;
|
|
|
|
if (Context.getCanonicalType(LContainer) ==
|
|
|
|
Context.getCanonicalType(RContainer))
|
|
|
|
MoreDerived = LContainer;
|
|
|
|
else if (IsDerivedFrom(LContainer, RContainer))
|
|
|
|
MoreDerived = LContainer;
|
|
|
|
else if (IsDerivedFrom(RContainer, LContainer))
|
|
|
|
MoreDerived = RContainer;
|
|
|
|
|
|
|
|
if (!MoreDerived.isNull()) {
|
|
|
|
// The type 'Q Pointee (MoreDerived::*)' is the common type.
|
|
|
|
// We don't use ImpCastExprToType here because this could still fail
|
|
|
|
// for ambiguous or inaccessible conversions.
|
2009-09-24 23:53:00 +04:00
|
|
|
LPointee = Context.getQualifiedType(LPointee, MergedQuals);
|
|
|
|
QualType Common
|
|
|
|
= Context.getMemberPointerType(LPointee, MoreDerived.getTypePtr());
|
2009-04-20 01:15:26 +04:00
|
|
|
if (PerformImplicitConversion(LHS, Common, "converting"))
|
|
|
|
return QualType();
|
|
|
|
if (PerformImplicitConversion(RHS, Common, "converting"))
|
|
|
|
return QualType();
|
|
|
|
return Common;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-16 21:51:27 +04:00
|
|
|
Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
|
|
|
|
<< LHS->getType() << RHS->getType()
|
|
|
|
<< LHS->getSourceRange() << RHS->getSourceRange();
|
|
|
|
return QualType();
|
|
|
|
}
|
2009-04-19 23:26:31 +04:00
|
|
|
|
|
|
|
/// \brief Find a merged pointer type and convert the two expressions to it.
|
|
|
|
///
|
2009-08-24 21:42:35 +04:00
|
|
|
/// This finds the composite pointer type (or member pointer type) for @p E1
|
|
|
|
/// and @p E2 according to C++0x 5.9p2. It converts both expressions to this
|
|
|
|
/// type and returns it.
|
2009-04-19 23:26:31 +04:00
|
|
|
/// It does not emit diagnostics.
|
|
|
|
QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) {
|
|
|
|
assert(getLangOptions().CPlusPlus && "This function assumes C++");
|
|
|
|
QualType T1 = E1->getType(), T2 = E2->getType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
if (!T1->isPointerType() && !T1->isMemberPointerType() &&
|
|
|
|
!T2->isPointerType() && !T2->isMemberPointerType())
|
|
|
|
return QualType();
|
2009-04-19 23:26:31 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
// FIXME: Do we need to work on the canonical types?
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-19 23:26:31 +04:00
|
|
|
// C++0x 5.9p2
|
|
|
|
// Pointer conversions and qualification conversions are performed on
|
|
|
|
// pointer operands to bring them to their composite pointer type. If
|
|
|
|
// one operand is a null pointer constant, the composite pointer type is
|
|
|
|
// the type of the other operand.
|
2009-09-25 08:25:58 +04:00
|
|
|
if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
|
2009-10-20 12:27:19 +04:00
|
|
|
if (T2->isMemberPointerType())
|
|
|
|
ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer);
|
|
|
|
else
|
|
|
|
ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer);
|
2009-04-19 23:26:31 +04:00
|
|
|
return T2;
|
|
|
|
}
|
2009-09-25 08:25:58 +04:00
|
|
|
if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
|
2009-10-20 12:27:19 +04:00
|
|
|
if (T1->isMemberPointerType())
|
|
|
|
ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer);
|
|
|
|
else
|
|
|
|
ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer);
|
2009-04-19 23:26:31 +04:00
|
|
|
return T1;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
// Now both have to be pointers or member pointers.
|
|
|
|
if (!T1->isPointerType() && !T1->isMemberPointerType() &&
|
|
|
|
!T2->isPointerType() && !T2->isMemberPointerType())
|
2009-04-19 23:26:31 +04:00
|
|
|
return QualType();
|
|
|
|
|
|
|
|
// Otherwise, of one of the operands has type "pointer to cv1 void," then
|
|
|
|
// the other has type "pointer to cv2 T" and the composite pointer type is
|
|
|
|
// "pointer to cv12 void," where cv12 is the union of cv1 and cv2.
|
|
|
|
// Otherwise, the composite pointer type is a pointer type similar to the
|
|
|
|
// type of one of the operands, with a cv-qualification signature that is
|
|
|
|
// the union of the cv-qualification signatures of the operand types.
|
|
|
|
// In practice, the first part here is redundant; it's subsumed by the second.
|
|
|
|
// What we do here is, we build the two possible composite types, and try the
|
|
|
|
// conversions in both directions. If only one works, or if the two composite
|
|
|
|
// types are the same, we have succeeded.
|
2009-09-24 23:53:00 +04:00
|
|
|
// FIXME: extended qualifiers?
|
2009-04-19 23:26:31 +04:00
|
|
|
llvm::SmallVector<unsigned, 4> QualifierUnion;
|
2009-08-24 21:42:35 +04:00
|
|
|
llvm::SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass;
|
2009-04-19 23:26:31 +04:00
|
|
|
QualType Composite1 = T1, Composite2 = T2;
|
2009-08-24 21:42:35 +04:00
|
|
|
do {
|
|
|
|
const PointerType *Ptr1, *Ptr2;
|
|
|
|
if ((Ptr1 = Composite1->getAs<PointerType>()) &&
|
|
|
|
(Ptr2 = Composite2->getAs<PointerType>())) {
|
|
|
|
Composite1 = Ptr1->getPointeeType();
|
|
|
|
Composite2 = Ptr2->getPointeeType();
|
|
|
|
QualifierUnion.push_back(
|
|
|
|
Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
|
|
|
|
MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0));
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
const MemberPointerType *MemPtr1, *MemPtr2;
|
|
|
|
if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) &&
|
|
|
|
(MemPtr2 = Composite2->getAs<MemberPointerType>())) {
|
|
|
|
Composite1 = MemPtr1->getPointeeType();
|
|
|
|
Composite2 = MemPtr2->getPointeeType();
|
|
|
|
QualifierUnion.push_back(
|
|
|
|
Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
|
|
|
|
MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(),
|
|
|
|
MemPtr2->getClass()));
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
// FIXME: block pointer types?
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
// Cannot unwrap any more types.
|
|
|
|
break;
|
|
|
|
} while (true);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 21:42:35 +04:00
|
|
|
// Rewrap the composites as pointers or member pointers with the union CVRs.
|
|
|
|
llvm::SmallVector<std::pair<const Type *, const Type *>, 4>::iterator MOC
|
|
|
|
= MemberOfClass.begin();
|
2009-09-09 19:08:12 +04:00
|
|
|
for (llvm::SmallVector<unsigned, 4>::iterator
|
2009-08-24 21:42:35 +04:00
|
|
|
I = QualifierUnion.begin(),
|
2009-09-09 19:08:12 +04:00
|
|
|
E = QualifierUnion.end();
|
2009-08-24 21:42:35 +04:00
|
|
|
I != E; (void)++I, ++MOC) {
|
2009-09-24 23:53:00 +04:00
|
|
|
Qualifiers Quals = Qualifiers::fromCVRMask(*I);
|
2009-08-24 21:42:35 +04:00
|
|
|
if (MOC->first && MOC->second) {
|
|
|
|
// Rebuild member pointer type
|
2009-09-24 23:53:00 +04:00
|
|
|
Composite1 = Context.getMemberPointerType(
|
|
|
|
Context.getQualifiedType(Composite1, Quals),
|
|
|
|
MOC->first);
|
|
|
|
Composite2 = Context.getMemberPointerType(
|
|
|
|
Context.getQualifiedType(Composite2, Quals),
|
|
|
|
MOC->second);
|
2009-08-24 21:42:35 +04:00
|
|
|
} else {
|
|
|
|
// Rebuild pointer type
|
2009-09-24 23:53:00 +04:00
|
|
|
Composite1
|
|
|
|
= Context.getPointerType(Context.getQualifiedType(Composite1, Quals));
|
|
|
|
Composite2
|
|
|
|
= Context.getPointerType(Context.getQualifiedType(Composite2, Quals));
|
2009-08-24 21:42:35 +04:00
|
|
|
}
|
2009-04-19 23:26:31 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
ImplicitConversionSequence E1ToC1 =
|
2009-08-27 21:24:15 +04:00
|
|
|
TryImplicitConversion(E1, Composite1,
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-09-09 19:08:12 +04:00
|
|
|
ImplicitConversionSequence E2ToC1 =
|
2009-08-27 21:24:15 +04:00
|
|
|
TryImplicitConversion(E2, Composite1,
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-19 23:26:31 +04:00
|
|
|
ImplicitConversionSequence E1ToC2, E2ToC2;
|
|
|
|
E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion;
|
|
|
|
E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion;
|
|
|
|
if (Context.getCanonicalType(Composite1) !=
|
|
|
|
Context.getCanonicalType(Composite2)) {
|
2009-08-27 21:24:15 +04:00
|
|
|
E1ToC2 = TryImplicitConversion(E1, Composite2,
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-08-27 21:24:15 +04:00
|
|
|
E2ToC2 = TryImplicitConversion(E2, Composite2,
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
/*AllowExplicit=*/false,
|
2009-08-28 19:33:32 +04:00
|
|
|
/*ForceRValue=*/false,
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-04-19 23:26:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ToC1Viable = E1ToC1.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion
|
|
|
|
&& E2ToC1.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion;
|
|
|
|
bool ToC2Viable = E1ToC2.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion
|
|
|
|
&& E2ToC2.ConversionKind !=
|
|
|
|
ImplicitConversionSequence::BadConversion;
|
|
|
|
if (ToC1Viable && !ToC2Viable) {
|
|
|
|
if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") &&
|
|
|
|
!PerformImplicitConversion(E2, Composite1, E2ToC1, "converting"))
|
|
|
|
return Composite1;
|
|
|
|
}
|
|
|
|
if (ToC2Viable && !ToC1Viable) {
|
|
|
|
if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") &&
|
|
|
|
!PerformImplicitConversion(E2, Composite2, E2ToC2, "converting"))
|
|
|
|
return Composite2;
|
|
|
|
}
|
|
|
|
return QualType();
|
|
|
|
}
|
2009-05-17 22:41:29 +04:00
|
|
|
|
2009-05-31 00:36:53 +04:00
|
|
|
Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) {
|
2009-08-16 03:41:35 +04:00
|
|
|
if (!Context.getLangOptions().CPlusPlus)
|
|
|
|
return Owned(E);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const RecordType *RT = E->getType()->getAs<RecordType>();
|
2009-05-31 00:36:53 +04:00
|
|
|
if (!RT)
|
|
|
|
return Owned(E);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-31 00:36:53 +04:00
|
|
|
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (RD->hasTrivialDestructor())
|
|
|
|
return Owned(E);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-14 05:30:44 +04:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(E)) {
|
|
|
|
QualType Ty = CE->getCallee()->getType();
|
|
|
|
if (const PointerType *PT = Ty->getAs<PointerType>())
|
|
|
|
Ty = PT->getPointeeType();
|
|
|
|
|
2009-09-22 03:43:11 +04:00
|
|
|
const FunctionType *FTy = Ty->getAs<FunctionType>();
|
2009-09-14 05:30:44 +04:00
|
|
|
if (FTy->getResultType()->isReferenceType())
|
|
|
|
return Owned(E);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
CXXTemporary *Temp = CXXTemporary::Create(Context,
|
2009-05-31 00:36:53 +04:00
|
|
|
RD->getDestructor(Context));
|
2009-05-31 01:21:49 +04:00
|
|
|
ExprTemporaries.push_back(Temp);
|
2009-08-03 23:13:25 +04:00
|
|
|
if (CXXDestructorDecl *Destructor =
|
|
|
|
const_cast<CXXDestructorDecl*>(RD->getDestructor(Context)))
|
|
|
|
MarkDeclarationReferenced(E->getExprLoc(), Destructor);
|
2009-05-31 00:36:53 +04:00
|
|
|
// FIXME: Add the temporary to the temporaries vector.
|
|
|
|
return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E));
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr,
|
2009-06-16 07:37:31 +04:00
|
|
|
bool ShouldDestroyTemps) {
|
2009-06-05 19:38:08 +04:00
|
|
|
assert(SubExpr && "sub expression can't be null!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 19:38:08 +04:00
|
|
|
if (ExprTemporaries.empty())
|
|
|
|
return SubExpr;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 19:38:08 +04:00
|
|
|
Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr,
|
2009-09-09 19:08:12 +04:00
|
|
|
&ExprTemporaries[0],
|
2009-06-05 19:38:08 +04:00
|
|
|
ExprTemporaries.size(),
|
2009-06-16 07:37:31 +04:00
|
|
|
ShouldDestroyTemps);
|
2009-06-05 19:38:08 +04:00
|
|
|
ExprTemporaries.clear();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 19:38:08 +04:00
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::OwningExprResult
|
2009-09-03 02:59:36 +04:00
|
|
|
Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc,
|
|
|
|
tok::TokenKind OpKind, TypeTy *&ObjectType) {
|
|
|
|
// Since this might be a postfix expression, get rid of ParenListExprs.
|
|
|
|
Base = MaybeConvertParenListExprToParenExpr(S, move(Base));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-03 02:59:36 +04:00
|
|
|
Expr *BaseExpr = (Expr*)Base.get();
|
|
|
|
assert(BaseExpr && "no record expansion");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-03 02:59:36 +04:00
|
|
|
QualType BaseType = BaseExpr->getType();
|
|
|
|
if (BaseType->isDependentType()) {
|
|
|
|
// FIXME: member of the current instantiation
|
|
|
|
ObjectType = BaseType.getAsOpaquePtr();
|
|
|
|
return move(Base);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-03 02:59:36 +04:00
|
|
|
// C++ [over.match.oper]p8:
|
2009-09-09 19:08:12 +04:00
|
|
|
// [...] When operator->returns, the operator-> is applied to the value
|
2009-09-03 02:59:36 +04:00
|
|
|
// returned, with the original second operand.
|
|
|
|
if (OpKind == tok::arrow) {
|
2009-09-30 05:01:30 +04:00
|
|
|
// The set of types we've considered so far.
|
2009-09-30 05:30:54 +04:00
|
|
|
llvm::SmallPtrSet<CanQualType,8> CTypes;
|
2009-09-30 21:46:20 +04:00
|
|
|
llvm::SmallVector<SourceLocation, 8> Locations;
|
2009-09-30 05:30:54 +04:00
|
|
|
CTypes.insert(Context.getCanonicalType(BaseType));
|
2009-09-30 05:01:30 +04:00
|
|
|
|
2009-09-03 02:59:36 +04:00
|
|
|
while (BaseType->isRecordType()) {
|
2009-10-14 02:43:21 +04:00
|
|
|
Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc);
|
2009-09-03 02:59:36 +04:00
|
|
|
BaseExpr = (Expr*)Base.get();
|
|
|
|
if (BaseExpr == NULL)
|
|
|
|
return ExprError();
|
2009-09-30 21:46:20 +04:00
|
|
|
if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr))
|
2009-10-14 02:55:59 +04:00
|
|
|
Locations.push_back(OpCall->getDirectCallee()->getLocation());
|
2009-09-30 05:01:30 +04:00
|
|
|
BaseType = BaseExpr->getType();
|
|
|
|
CanQualType CBaseType = Context.getCanonicalType(BaseType);
|
2009-09-30 05:30:54 +04:00
|
|
|
if (!CTypes.insert(CBaseType)) {
|
2009-09-30 04:19:41 +04:00
|
|
|
Diag(OpLoc, diag::err_operator_arrow_circular);
|
2009-09-30 21:46:20 +04:00
|
|
|
for (unsigned i = 0; i < Locations.size(); i++)
|
|
|
|
Diag(Locations[i], diag::note_declared_at);
|
2009-09-30 04:19:41 +04:00
|
|
|
return ExprError();
|
|
|
|
}
|
2009-09-03 02:59:36 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-03 02:59:36 +04:00
|
|
|
if (BaseType->isPointerType())
|
|
|
|
BaseType = BaseType->getPointeeType();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// We could end up with various non-record types here, such as extended
|
2009-09-03 02:59:36 +04:00
|
|
|
// vector types or Objective-C interfaces. Just return early and let
|
|
|
|
// ActOnMemberReferenceExpr do the work.
|
2009-09-04 01:38:09 +04:00
|
|
|
if (!BaseType->isRecordType()) {
|
|
|
|
// C++ [basic.lookup.classref]p2:
|
|
|
|
// [...] If the type of the object expression is of pointer to scalar
|
|
|
|
// type, the unqualified-id is looked up in the context of the complete
|
|
|
|
// postfix-expression.
|
|
|
|
ObjectType = 0;
|
2009-09-03 02:59:36 +04:00
|
|
|
return move(Base);
|
2009-09-04 01:38:09 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-04 01:38:09 +04:00
|
|
|
// C++ [basic.lookup.classref]p2:
|
2009-09-09 19:08:12 +04:00
|
|
|
// If the id-expression in a class member access (5.2.5) is an
|
2009-09-04 01:38:09 +04:00
|
|
|
// unqualified-id, and the type of the object expres- sion is of a class
|
|
|
|
// type C (or of pointer to a class type C), the unqualified-id is looked
|
|
|
|
// up in the scope of class C. [...]
|
2009-09-03 02:59:36 +04:00
|
|
|
ObjectType = BaseType.getAsOpaquePtr();
|
2009-09-09 19:08:12 +04:00
|
|
|
return move(Base);
|
2009-09-03 02:59:36 +04:00
|
|
|
}
|
|
|
|
|
2009-09-29 03:23:40 +04:00
|
|
|
CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp,
|
|
|
|
CXXMethodDecl *Method) {
|
|
|
|
MemberExpr *ME =
|
|
|
|
new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method,
|
|
|
|
SourceLocation(), Method->getType());
|
|
|
|
QualType ResultType;
|
|
|
|
if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Method))
|
|
|
|
ResultType = Conv->getConversionType().getNonReferenceType();
|
|
|
|
else
|
|
|
|
ResultType = Method->getResultType().getNonReferenceType();
|
|
|
|
|
|
|
|
CXXMemberCallExpr *CE =
|
|
|
|
new (Context) CXXMemberCallExpr(Context, ME, 0, 0,
|
|
|
|
ResultType,
|
|
|
|
SourceLocation());
|
|
|
|
return CE;
|
|
|
|
}
|
|
|
|
|
2009-09-10 01:33:21 +04:00
|
|
|
Sema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc,
|
|
|
|
QualType Ty,
|
|
|
|
CastExpr::CastKind Kind,
|
|
|
|
CXXMethodDecl *Method,
|
|
|
|
ExprArg Arg) {
|
|
|
|
Expr *From = Arg.takeAs<Expr>();
|
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unhandled cast kind!");
|
|
|
|
case CastExpr::CK_ConstructorConversion: {
|
2009-09-10 03:08:42 +04:00
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
|
|
|
|
|
|
|
|
if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method),
|
|
|
|
MultiExprArg(*this, (void **)&From, 1),
|
|
|
|
CastLoc, ConstructorArgs))
|
|
|
|
return ExprError();
|
2009-10-19 01:20:14 +04:00
|
|
|
|
|
|
|
OwningExprResult Result =
|
|
|
|
BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method),
|
|
|
|
move_arg(ConstructorArgs));
|
|
|
|
if (Result.isInvalid())
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
return MaybeBindToTemporary(Result.takeAs<Expr>());
|
2009-09-10 01:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case CastExpr::CK_UserDefinedConversion: {
|
2009-09-15 11:42:44 +04:00
|
|
|
assert(!From->getType()->isPointerType() && "Arg can't have pointer type!");
|
|
|
|
|
|
|
|
// Cast to base if needed.
|
|
|
|
if (PerformObjectArgumentInitialization(From, Method))
|
|
|
|
return ExprError();
|
|
|
|
|
2009-09-29 03:23:40 +04:00
|
|
|
// Create an implicit call expr that calls it.
|
|
|
|
CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method);
|
2009-10-19 01:20:14 +04:00
|
|
|
return MaybeBindToTemporary(CE);
|
2009-09-10 01:33:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 22:41:29 +04:00
|
|
|
Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) {
|
|
|
|
Expr *FullExpr = Arg.takeAs<Expr>();
|
2009-06-05 19:38:08 +04:00
|
|
|
if (FullExpr)
|
2009-09-09 19:08:12 +04:00
|
|
|
FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr,
|
2009-06-16 07:37:31 +04:00
|
|
|
/*ShouldDestroyTemps=*/true);
|
2009-05-17 22:41:29 +04:00
|
|
|
|
2009-08-26 03:46:41 +04:00
|
|
|
|
2009-05-17 22:41:29 +04:00
|
|
|
return Owned(FullExpr);
|
|
|
|
}
|
2009-10-22 11:08:30 +04:00
|
|
|
|
|
|
|
/// \brief Determine whether a reference to the given declaration in the
|
|
|
|
/// current context is an implicit member access
|
|
|
|
/// (C++ [class.mfct.non-static]p2).
|
|
|
|
///
|
|
|
|
/// FIXME: Should Objective-C also use this approach?
|
|
|
|
///
|
|
|
|
/// \param SS if non-NULL, the C++ nested-name-specifier that precedes the
|
|
|
|
/// name of the declaration referenced.
|
|
|
|
///
|
|
|
|
/// \param D the declaration being referenced from the current scope.
|
|
|
|
///
|
|
|
|
/// \param NameLoc the location of the name in the source.
|
|
|
|
///
|
|
|
|
/// \param ThisType if the reference to this declaration is an implicit member
|
|
|
|
/// access, will be set to the type of the "this" pointer to be used when
|
|
|
|
/// building that implicit member access.
|
|
|
|
///
|
|
|
|
/// \param MemberType if the reference to this declaration is an implicit
|
|
|
|
/// member access, will be set to the type of the member being referenced
|
|
|
|
/// (for use at the type of the resulting member access expression).
|
|
|
|
///
|
|
|
|
/// \returns true if this is an implicit member reference (in which case
|
|
|
|
/// \p ThisType and \p MemberType will be set), or false if it is not an
|
|
|
|
/// implicit member reference.
|
|
|
|
bool Sema::isImplicitMemberReference(const CXXScopeSpec *SS, NamedDecl *D,
|
|
|
|
SourceLocation NameLoc, QualType &ThisType,
|
|
|
|
QualType &MemberType) {
|
|
|
|
// If this isn't a C++ method, then it isn't an implicit member reference.
|
|
|
|
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext);
|
|
|
|
if (!MD || MD->isStatic())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// C++ [class.mfct.nonstatic]p2:
|
|
|
|
// [...] if name lookup (3.4.1) resolves the name in the
|
|
|
|
// id-expression to a nonstatic nontype member of class X or of
|
|
|
|
// a base class of X, the id-expression is transformed into a
|
|
|
|
// class member access expression (5.2.5) using (*this) (9.3.2)
|
|
|
|
// as the postfix-expression to the left of the '.' operator.
|
|
|
|
DeclContext *Ctx = 0;
|
|
|
|
if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
|
|
|
|
Ctx = FD->getDeclContext();
|
|
|
|
MemberType = FD->getType();
|
|
|
|
|
|
|
|
if (const ReferenceType *RefType = MemberType->getAs<ReferenceType>())
|
|
|
|
MemberType = RefType->getPointeeType();
|
|
|
|
else if (!FD->isMutable())
|
|
|
|
MemberType
|
|
|
|
= Context.getQualifiedType(MemberType,
|
|
|
|
Qualifiers::fromCVRMask(MD->getTypeQualifiers()));
|
|
|
|
} else {
|
|
|
|
for (OverloadIterator Ovl(D), OvlEnd; Ovl != OvlEnd; ++Ovl) {
|
|
|
|
CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Ovl);
|
|
|
|
FunctionTemplateDecl *FunTmpl = 0;
|
|
|
|
if (!Method && (FunTmpl = dyn_cast<FunctionTemplateDecl>(*Ovl)))
|
|
|
|
Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
|
|
|
|
|
2009-10-24 08:59:53 +04:00
|
|
|
// FIXME: Do we have to know if there are explicit template arguments?
|
2009-10-22 11:08:30 +04:00
|
|
|
if (Method && !Method->isStatic()) {
|
|
|
|
Ctx = Method->getParent();
|
|
|
|
if (isa<CXXMethodDecl>(D) && !FunTmpl)
|
|
|
|
MemberType = Method->getType();
|
|
|
|
else
|
|
|
|
MemberType = Context.OverloadTy;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ctx || !Ctx->isRecord())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Determine whether the declaration(s) we found are actually in a base
|
|
|
|
// class. If not, this isn't an implicit member reference.
|
|
|
|
ThisType = MD->getThisType(Context);
|
2009-11-01 20:08:18 +03:00
|
|
|
|
|
|
|
// If the type of "this" is dependent, we can't tell if the member is in a
|
|
|
|
// base class or not, so treat this as a dependent implicit member reference.
|
|
|
|
if (ThisType->isDependentType())
|
|
|
|
return true;
|
|
|
|
|
2009-10-22 11:08:30 +04:00
|
|
|
QualType CtxType = Context.getTypeDeclType(cast<CXXRecordDecl>(Ctx));
|
|
|
|
QualType ClassType
|
|
|
|
= Context.getTypeDeclType(cast<CXXRecordDecl>(MD->getParent()));
|
|
|
|
return Context.hasSameType(CtxType, ClassType) ||
|
|
|
|
IsDerivedFrom(ClassType, CtxType);
|
|
|
|
}
|
|
|
|
|