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"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2007-08-25 18:02:58 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-09-10 06:17:11 +04:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2008-10-07 03:16:35 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-08-11 07:27:53 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-11-19 22:09:45 +03:00
|
|
|
/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
|
2008-11-17 23:34:05 +03:00
|
|
|
/// name (e.g., operator void const *) as an expression. This is
|
|
|
|
/// very similar to ActOnIdentifierExpr, except that instead of
|
|
|
|
/// providing an identifier the parser provides the type of the
|
|
|
|
/// conversion function.
|
2008-11-19 22:09:45 +03:00
|
|
|
Sema::ExprResult
|
|
|
|
Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
|
|
|
|
TypeTy *Ty, bool HasTrailingLParen,
|
|
|
|
const CXXScopeSpec &SS) {
|
2008-11-17 23:34:05 +03:00
|
|
|
QualType ConvType = QualType::getFromOpaquePtr(Ty);
|
|
|
|
QualType ConvTypeCanon = Context.getCanonicalType(ConvType);
|
|
|
|
DeclarationName ConvName
|
|
|
|
= Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
|
2008-11-18 18:03:34 +03:00
|
|
|
return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
|
2008-11-19 22:09:45 +03:00
|
|
|
&SS);
|
2008-11-17 23:34:05 +03:00
|
|
|
}
|
2008-11-11 14:37:55 +03:00
|
|
|
|
2008-11-19 22:09:45 +03:00
|
|
|
/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
/// name (e.g., @c operator+ ) as an expression. This is very
|
|
|
|
/// similar to ActOnIdentifierExpr, except that instead of providing
|
|
|
|
/// an identifier the parser provides the kind of overloaded
|
|
|
|
/// operator that was parsed.
|
2008-11-19 22:09:45 +03:00
|
|
|
Sema::ExprResult
|
|
|
|
Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
|
|
|
|
OverloadedOperatorKind Op,
|
|
|
|
bool HasTrailingLParen,
|
|
|
|
const CXXScopeSpec &SS) {
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
|
2008-11-19 22:09:45 +03:00
|
|
|
return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS);
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
}
|
|
|
|
|
2008-11-11 14:37:55 +03:00
|
|
|
/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
|
|
|
|
Action::ExprResult
|
|
|
|
Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
|
|
|
|
bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
|
|
|
|
const NamespaceDecl *StdNs = GetStdNamespace();
|
|
|
|
if (!StdNs) {
|
|
|
|
Diag(OpLoc, diag::err_need_header_before_typeid);
|
|
|
|
return ExprResult(true);
|
|
|
|
}
|
|
|
|
if (!Ident_TypeInfo) {
|
|
|
|
Ident_TypeInfo = &PP.getIdentifierTable().get("type_info");
|
|
|
|
}
|
|
|
|
Decl *TypeInfoDecl = LookupDecl(Ident_TypeInfo,
|
|
|
|
Decl::IDNS_Tag | Decl::IDNS_Ordinary,
|
|
|
|
0, StdNs, /*createBuiltins=*/false);
|
|
|
|
RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
|
|
|
|
if (!TypeInfoRecordDecl) {
|
|
|
|
Diag(OpLoc, diag::err_need_header_before_typeid);
|
|
|
|
return ExprResult(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
|
|
|
|
|
|
|
|
return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(),
|
|
|
|
SourceRange(OpLoc, RParenLoc));
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
/// ActOnCXXBoolLiteral - Parse {true,false} literals.
|
2007-07-11 21:01:13 +04:00
|
|
|
Action::ExprResult
|
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!");
|
2007-08-25 18:02:58 +04:00
|
|
|
return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-02-26 03:51:44 +03:00
|
|
|
|
|
|
|
/// ActOnCXXThrow - Parse throw expressions.
|
|
|
|
Action::ExprResult
|
|
|
|
Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
|
|
|
|
return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
|
|
|
|
}
|
2008-07-01 14:37:29 +04:00
|
|
|
|
|
|
|
Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
|
|
|
|
/// 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.
|
|
|
|
|
|
|
|
if (!isa<FunctionDecl>(CurContext)) {
|
|
|
|
Diag(ThisLoc, diag::err_invalid_this_use);
|
|
|
|
return ExprResult(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
|
|
|
|
if (MD->isInstance())
|
2008-11-04 17:32:21 +03:00
|
|
|
return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
|
2008-07-01 14:37:29 +04:00
|
|
|
|
|
|
|
return Diag(ThisLoc, diag::err_invalid_this_use);
|
|
|
|
}
|
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()").
|
|
|
|
Action::ExprResult
|
|
|
|
Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
ExprTy **ExprTys, unsigned NumExprs,
|
|
|
|
SourceLocation *CommaLocs,
|
|
|
|
SourceLocation RParenLoc) {
|
|
|
|
assert(TypeRep && "Missing type!");
|
|
|
|
QualType Ty = QualType::getFromOpaquePtr(TypeRep);
|
|
|
|
Expr **Exprs = (Expr**)ExprTys;
|
|
|
|
SourceLocation TyBeginLoc = TypeRange.getBegin();
|
|
|
|
SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
|
|
|
|
|
|
|
|
if (const RecordType *RT = Ty->getAsRecordType()) {
|
|
|
|
// C++ 5.2.3p1:
|
|
|
|
// If the simple-type-specifier specifies a class type, the class type shall
|
|
|
|
// be complete.
|
|
|
|
//
|
|
|
|
if (!RT->getDecl()->isDefinition())
|
|
|
|
return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
|
|
|
|
Ty.getAsString(), FullRange);
|
|
|
|
|
2008-10-07 03:16:35 +04:00
|
|
|
unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error,
|
|
|
|
"class constructors are not supported yet");
|
|
|
|
return Diag(TyBeginLoc, DiagID);
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++ 5.2.3p1:
|
|
|
|
// 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) {
|
|
|
|
if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
|
|
|
|
return true;
|
2008-10-27 22:41:14 +03:00
|
|
|
return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
|
|
|
|
Exprs[0], RParenLoc);
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++ 5.2.3p1:
|
|
|
|
// If the expression list specifies more than a single value, the type shall
|
|
|
|
// be a class with a suitably declared constructor.
|
|
|
|
//
|
|
|
|
if (NumExprs > 1)
|
2008-11-19 08:27:50 +03:00
|
|
|
return 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");
|
|
|
|
|
|
|
|
// C++ 5.2.3p2:
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
if (Ty->isArrayType())
|
2008-11-19 08:27:50 +03:00
|
|
|
return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange;
|
2008-08-22 19:38:55 +04:00
|
|
|
if (Ty->isIncompleteType() && !Ty->isVoidType())
|
2008-11-19 08:27:50 +03:00
|
|
|
return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use)
|
|
|
|
<< Ty.getAsString() << FullRange;
|
2008-08-22 19:38:55 +04:00
|
|
|
|
|
|
|
return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
|
|
|
|
}
|
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()) {...}"
|
|
|
|
Action::ExprResult
|
|
|
|
Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
|
|
|
|
Declarator &D,
|
|
|
|
SourceLocation EqualLoc,
|
|
|
|
ExprTy *AssignExprVal) {
|
|
|
|
assert(AssignExprVal && "Null assignment expression");
|
|
|
|
|
|
|
|
// 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.");
|
|
|
|
|
|
|
|
QualType Ty = GetTypeForDeclarator(D, S);
|
|
|
|
|
|
|
|
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.
|
2008-11-19 08:27:50 +03:00
|
|
|
return 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);
|
2008-09-10 06:17:11 +04:00
|
|
|
} else if (const RecordType *RT = Ty->getAsRecordType()) {
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
// The type-specifier-seq shall not declare a new class...
|
|
|
|
if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
|
|
|
|
Diag(RD->getLocation(), diag::err_type_defined_in_condition);
|
|
|
|
} else if (const EnumType *ET = Ty->getAsEnumType()) {
|
|
|
|
EnumDecl *ED = ET->getDecl();
|
|
|
|
// ...or enumeration.
|
|
|
|
if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
|
|
|
|
Diag(ED->getLocation(), diag::err_type_defined_in_condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclTy *Dcl = ActOnDeclarator(S, D, 0);
|
|
|
|
if (!Dcl)
|
|
|
|
return true;
|
|
|
|
AddInitializerToDecl(Dcl, AssignExprVal);
|
|
|
|
|
|
|
|
return new CXXConditionDeclExpr(StartLoc, EqualLoc,
|
|
|
|
cast<VarDecl>(static_cast<Decl *>(Dcl)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
//
|
|
|
|
QualType Ty = CondExpr->getType(); // Save the type.
|
|
|
|
AssignConvertType
|
|
|
|
ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr);
|
|
|
|
if (ConvTy == Incompatible)
|
|
|
|
return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition,
|
|
|
|
Ty.getAsString(), CondExpr->getSourceRange());
|
|
|
|
return false;
|
|
|
|
}
|
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).
|
|
|
|
bool
|
|
|
|
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))
|
|
|
|
if (const PointerType *ToPtrType = ToType->getAsPointerType())
|
|
|
|
if (const BuiltinType *ToPointeeType
|
|
|
|
= ToPtrType->getPointeeType()->getAsBuiltinType()) {
|
|
|
|
// This conversion is considered only when there is an
|
|
|
|
// explicit appropriate pointer target type (C++ 4.2p2).
|
|
|
|
if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
|
|
|
|
((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
|
|
|
|
/// converted expression.
|
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType)
|
|
|
|
{
|
2008-10-29 03:13:59 +03:00
|
|
|
ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType);
|
2008-10-24 08:54:22 +04:00
|
|
|
switch (ICS.ConversionKind) {
|
|
|
|
case ImplicitConversionSequence::StandardConversion:
|
|
|
|
if (PerformImplicitConversion(From, ToType, ICS.Standard))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ImplicitConversionSequence::UserDefinedConversion:
|
|
|
|
// FIXME: This is, of course, wrong. We'll need to actually call
|
|
|
|
// the constructor or conversion operator, and then cope with the
|
|
|
|
// standard conversions.
|
|
|
|
ImpCastExprToType(From, ToType);
|
2008-10-31 19:23:19 +03:00
|
|
|
return false;
|
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
|
|
|
|
/// expression.
|
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
|
|
|
const StandardConversionSequence& SCS)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
QualType FromType = From->getType();
|
|
|
|
|
2008-11-03 22:09:14 +03:00
|
|
|
if (SCS.CopyConstructor) {
|
|
|
|
// FIXME: Create a temporary object by calling the copy
|
|
|
|
// constructor.
|
|
|
|
ImpCastExprToType(From, ToType);
|
|
|
|
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:
|
2008-11-10 23:40:00 +03:00
|
|
|
if (FromType->isOverloadType()) {
|
|
|
|
FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
|
|
|
|
if (!Fn)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
FixOverloadedFunctionReference(From, Fn);
|
|
|
|
FromType = From->getType();
|
|
|
|
} else {
|
|
|
|
FromType = Context.getArrayDecayedType(FromType);
|
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Function_To_Pointer:
|
|
|
|
FromType = Context.getPointerType(FromType);
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper first standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the second implicit conversion
|
|
|
|
switch (SCS.Second) {
|
|
|
|
case ICK_Identity:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Integral_Promotion:
|
|
|
|
case ICK_Floating_Promotion:
|
|
|
|
case ICK_Integral_Conversion:
|
|
|
|
case ICK_Floating_Conversion:
|
|
|
|
case ICK_Floating_Integral:
|
|
|
|
FromType = ToType.getUnqualifiedType();
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Pointer_Conversion:
|
|
|
|
if (CheckPointerConversion(From, ToType))
|
|
|
|
return true;
|
|
|
|
ImpCastExprToType(From, ToType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Pointer_Member:
|
|
|
|
// FIXME: Implement pointer-to-member conversions.
|
|
|
|
assert(false && "Pointer-to-member conversions are unsupported");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Boolean_Conversion:
|
|
|
|
FromType = Context.BoolTy;
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SCS.Third) {
|
|
|
|
case ICK_Identity:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Qualification:
|
|
|
|
ImpCastExprToType(From, ToType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|