implement support for casts to/from pointers.

llvm-svn: 39595
This commit is contained in:
Chris Lattner 2007-06-06 04:05:39 +00:00
parent 6563712933
commit cf106ab42a
4 changed files with 24 additions and 30 deletions

View File

@ -39,16 +39,29 @@ Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
/// the type specified by DstTy, following the rules of C99 6.3.
RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
QualType DstTy) {
QualType DstTy, SourceLocation Loc) {
ValTy = ValTy.getCanonicalType();
DstTy = DstTy.getCanonicalType();
if (ValTy == DstTy) return Val;
if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) {
if (isa<PointerType>(DstTy)) {
const llvm::Type *DestTy = ConvertType(DstTy, Loc);
// The source value may be an integer, or a pointer.
assert(Val.isScalar() && "Can only convert from integer or pointer");
if (isa<llvm::PointerType>(Val.getVal()->getType()))
return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
} else if (isa<PointerType>(ValTy)) {
// Must be an ptr to int cast.
const llvm::Type *DestTy = ConvertType(DstTy, Loc);
assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
} else if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) {
if (DestBT->getKind() == BuiltinType::Bool)
return RValue::get(ConvertScalarValueToBool(Val, ValTy));
}
assert(0 && "FIXME: Unsupported conversion!");
}
@ -261,7 +274,7 @@ RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
if (E->getType()->isVoidType())
return RValue::getAggregate(0);
return EmitConversion(Src, SrcTy, E->getType());
return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
}
//===----------------------------------------------------------------------===//
@ -705,7 +718,8 @@ RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
// Convert the RHS to the type of the LHS.
RHS = EmitConversion(RHS, RHSTy, E->getType());
// FIXME: I'm not thrilled about having to call getLocStart() here... :(
RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
// Store the value into the LHS.
EmitStoreThroughLValue(RHS, LHS, E->getType());

View File

@ -161,7 +161,8 @@ public:
/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
/// the type specified by DstTy, following the rules of C99 6.3.
RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
SourceLocation Loc);
/// ConvertScalarValueToBool - Convert the specified expression value to a
/// boolean (i1) truth value. This is equivalent to "Val == 0".

View File

@ -106,23 +106,6 @@
DED7D9E50A5257F6003AD0FB /* ScratchBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DED7D9E40A5257F6003AD0FB /* ScratchBuffer.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXBuildStyle section */
84916BDA0C15E9B20080778F /* Development */ = {
isa = PBXBuildStyle;
buildSettings = {
COPY_PHASE_STRIP = NO;
};
name = Development;
};
84916BDB0C15E9B20080778F /* Deployment */ = {
isa = PBXBuildStyle;
buildSettings = {
COPY_PHASE_STRIP = YES;
};
name = Deployment;
};
/* End PBXBuildStyle section */
/* Begin PBXCopyFilesBuildPhase section */
8DD76F690486A84900D96B5E /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
@ -186,7 +169,7 @@
1A30A9E80B93A4C800201A91 /* ExprCXX.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ExprCXX.h; path = clang/AST/ExprCXX.h; sourceTree = "<group>"; };
1A869A6E0BA2164C008DA07A /* LiteralSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LiteralSupport.h; sourceTree = "<group>"; };
1A869AA70BA21ABA008DA07A /* LiteralSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = LiteralSupport.cpp; sourceTree = "<group>"; };
8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
@ -564,12 +547,6 @@
08FB7793FE84155DC02AAC07 /* Project object */ = {
isa = PBXProject;
buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
buildSettings = {
};
buildStyles = (
84916BDA0C15E9B20080778F /* Development */,
84916BDB0C15E9B20080778F /* Deployment */,
);
hasScannedForEncodings = 1;
mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
projectDirPath = "";

View File

@ -412,6 +412,8 @@ public:
CastExpr(StmtClass SC, QualType ty, Expr *op) :
Expr(SC, QualType()), Ty(ty), Op(op), Loc(SourceLocation()) {}
SourceLocation getLParenLoc() const { return Loc; }
QualType getDestType() const { return Ty; }
Expr *getSubExpr() const { return Op; }
virtual SourceRange getSourceRange() const {