Skip to content

Commit 8cb679e

Browse files
committed
Assorted work leading towards the elimination of CK_Unknown.
llvm-svn: 119138
1 parent 14776cf commit 8cb679e

18 files changed

+440
-187
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ class CastExpr : public Expr {
19611961
private:
19621962
Stmt *Op;
19631963

1964-
void CheckBasePath() const {
1964+
void CheckCastConsistency() const {
19651965
#ifndef NDEBUG
19661966
switch (getCastKind()) {
19671967
case CK_DerivedToBase:
@@ -1973,17 +1973,14 @@ class CastExpr : public Expr {
19731973
break;
19741974

19751975
// These should not have an inheritance path.
1976-
case CK_Unknown:
19771976
case CK_BitCast:
19781977
case CK_LValueBitCast:
1979-
case CK_NoOp:
19801978
case CK_Dynamic:
19811979
case CK_ToUnion:
19821980
case CK_ArrayToPointerDecay:
19831981
case CK_FunctionToPointerDecay:
19841982
case CK_NullToMemberPointer:
19851983
case CK_NullToPointer:
1986-
case CK_UserDefinedConversion:
19871984
case CK_ConstructorConversion:
19881985
case CK_IntegralToPointer:
19891986
case CK_PointerToIntegral:
@@ -1993,20 +1990,30 @@ class CastExpr : public Expr {
19931990
case CK_IntegralToFloating:
19941991
case CK_FloatingToIntegral:
19951992
case CK_FloatingCast:
1996-
case CK_MemberPointerToBoolean:
19971993
case CK_AnyPointerToObjCPointerCast:
19981994
case CK_AnyPointerToBlockPointerCast:
19991995
case CK_ObjCObjectLValueCast:
20001996
case CK_FloatingRealToComplex:
20011997
case CK_FloatingComplexToReal:
2002-
case CK_FloatingComplexToBoolean:
20031998
case CK_FloatingComplexCast:
20041999
case CK_FloatingComplexToIntegralComplex:
20052000
case CK_IntegralRealToComplex:
20062001
case CK_IntegralComplexToReal:
2007-
case CK_IntegralComplexToBoolean:
20082002
case CK_IntegralComplexCast:
20092003
case CK_IntegralComplexToFloatingComplex:
2004+
assert(!getType()->isBooleanType() && "unheralded conversion to bool");
2005+
// fallthrough to check for null base path
2006+
2007+
case CK_Dependent:
2008+
case CK_Unknown:
2009+
case CK_NoOp:
2010+
case CK_UserDefinedConversion:
2011+
case CK_PointerToBoolean:
2012+
case CK_IntegralToBoolean:
2013+
case CK_FloatingToBoolean:
2014+
case CK_MemberPointerToBoolean:
2015+
case CK_FloatingComplexToBoolean:
2016+
case CK_IntegralComplexToBoolean:
20102017
assert(path_empty() && "Cast kind should not have a base path!");
20112018
break;
20122019
}
@@ -2029,9 +2036,10 @@ class CastExpr : public Expr {
20292036
// dependent or if the subexpression is value-dependent.
20302037
ty->isDependentType() || (op && op->isValueDependent())),
20312038
Op(op) {
2039+
assert(kind != CK_Invalid && "creating cast with invalid cast kind");
20322040
CastExprBits.Kind = kind;
20332041
CastExprBits.BasePathSize = BasePathSize;
2034-
CheckBasePath();
2042+
CheckCastConsistency();
20352043
}
20362044

20372045
/// \brief Construct an empty cast.

clang/include/clang/AST/OperationKinds.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ enum CastKind {
2525
/// going on.
2626
CK_Unknown,
2727

28+
/// CK_Dependent - This explicit cast cannot yet be analyzed because
29+
/// the type or expression is dependent.
30+
CK_Dependent,
31+
2832
/// CK_BitCast - Used for reinterpret_cast.
2933
CK_BitCast,
3034

@@ -83,6 +87,9 @@ enum CastKind {
8387

8488
/// CK_PointerToIntegral - Pointer to integral
8589
CK_PointerToIntegral,
90+
91+
/// CK_PointerToBoolean - Pointer to boolean (i.e. is not null)
92+
CK_PointerToBoolean,
8693

8794
/// CK_ToVoid - Cast to void.
8895
CK_ToVoid,
@@ -92,14 +99,21 @@ enum CastKind {
9299
/// src expression into the destination expression.
93100
CK_VectorSplat,
94101

95-
/// CK_IntegralCast - Casting between integral types of different size.
102+
/// CK_IntegralCast - A truncating or extending cast between integral
103+
/// types of different size.
96104
CK_IntegralCast,
97105

106+
/// CK_IntegralToBoolean - Integral to boolean.
107+
CK_IntegralToBoolean,
108+
98109
/// CK_IntegralToFloating - Integral to floating point.
99110
CK_IntegralToFloating,
100111

101112
/// CK_FloatingToIntegral - Floating point to integral.
102113
CK_FloatingToIntegral,
114+
115+
/// CK_FloatingToBoolean - Floating point to boolean.
116+
CK_FloatingToBoolean,
103117

104118
/// CK_FloatingCast - Casting between floating types of different size.
105119
CK_FloatingCast,
@@ -151,6 +165,7 @@ enum CastKind {
151165
CK_IntegralComplexToFloatingComplex
152166
};
153167

168+
#define CK_Invalid ((CastKind) -1)
154169

155170
enum BinaryOperatorKind {
156171
// Operators listed in order of precedence.

clang/include/clang/AST/Type.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,18 @@ class Type {
11231123
bool isTemplateTypeParmType() const; // C++ template type parameter
11241124
bool isNullPtrType() const; // C++0x nullptr_t
11251125

1126+
enum ScalarTypeKind {
1127+
STK_Pointer,
1128+
STK_MemberPointer,
1129+
STK_Bool,
1130+
STK_Integral,
1131+
STK_Floating,
1132+
STK_IntegralComplex,
1133+
STK_FloatingComplex
1134+
};
1135+
/// getScalarTypeKind - Given that this is a scalar type, classify it.
1136+
ScalarTypeKind getScalarTypeKind() const;
1137+
11261138
/// isDependentType - Whether this type is a dependent type, meaning
11271139
/// that its definition somehow depends on a template parameter
11281140
/// (C++ [temp.dep.type]).

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,8 @@ class Sema {
41094109
/// CheckAssignmentConstraints - Perform type checking for assignment,
41104110
/// argument passing, variable initialization, and function return values.
41114111
/// This routine is only used by the following two methods. C99 6.5.16.
4112-
AssignConvertType CheckAssignmentConstraints(QualType lhs, QualType rhs);
4112+
AssignConvertType CheckAssignmentConstraints(QualType lhs, QualType rhs,
4113+
CastKind &Kind);
41134114

41144115
// CheckSingleAssignmentConstraints - Currently used by
41154116
// CheckAssignmentOperands, and ActOnReturnStmt. Prior to type checking,

clang/lib/AST/Expr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ const char *CastExpr::getCastKindName() const {
742742
switch (getCastKind()) {
743743
case CK_Unknown:
744744
return "Unknown";
745+
case CK_Dependent:
746+
return "Dependent";
745747
case CK_BitCast:
746748
return "BitCast";
747749
case CK_LValueBitCast:
@@ -778,18 +780,24 @@ const char *CastExpr::getCastKindName() const {
778780
return "IntegralToPointer";
779781
case CK_PointerToIntegral:
780782
return "PointerToIntegral";
783+
case CK_PointerToBoolean:
784+
return "PointerToBoolean";
781785
case CK_ToVoid:
782786
return "ToVoid";
783787
case CK_VectorSplat:
784788
return "VectorSplat";
785789
case CK_IntegralCast:
786790
return "IntegralCast";
791+
case CK_IntegralToBoolean:
792+
return "IntegralToBoolean";
787793
case CK_IntegralToFloating:
788794
return "IntegralToFloating";
789795
case CK_FloatingToIntegral:
790796
return "FloatingToIntegral";
791797
case CK_FloatingCast:
792798
return "FloatingCast";
799+
case CK_FloatingToBoolean:
800+
return "FloatingToBoolean";
793801
case CK_MemberPointerToBoolean:
794802
return "MemberPointerToBoolean";
795803
case CK_AnyPointerToObjCPointerCast:

clang/lib/AST/Type.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ bool Type::isArithmeticType() const {
572572

573573
bool Type::isScalarType() const {
574574
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575-
return BT->getKind() != BuiltinType::Void && !BT->isPlaceholderType();
575+
return BT->getKind() > BuiltinType::Void &&
576+
BT->getKind() <= BuiltinType::NullPtr;
576577
if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
577578
// Enums are scalar types, but only if they are defined. Incomplete enums
578579
// are not treated as scalar types.
@@ -584,6 +585,35 @@ bool Type::isScalarType() const {
584585
isa<ObjCObjectPointerType>(CanonicalType);
585586
}
586587

588+
Type::ScalarTypeKind Type::getScalarTypeKind() const {
589+
assert(isScalarType());
590+
591+
const Type *T = CanonicalType.getTypePtr();
592+
if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
593+
if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
594+
if (BT->getKind() == BuiltinType::NullPtr) return STK_Pointer;
595+
if (BT->isInteger()) return STK_Integral;
596+
if (BT->isFloatingPoint()) return STK_Floating;
597+
llvm_unreachable("unknown scalar builtin type");
598+
} else if (isa<PointerType>(T) ||
599+
isa<BlockPointerType>(T) ||
600+
isa<ObjCObjectPointerType>(T)) {
601+
return STK_Pointer;
602+
} else if (isa<MemberPointerType>(T)) {
603+
return STK_MemberPointer;
604+
} else if (isa<EnumType>(T)) {
605+
assert(cast<EnumType>(T)->getDecl()->isComplete());
606+
return STK_Integral;
607+
} else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
608+
if (CT->getElementType()->isRealFloatingType())
609+
return STK_FloatingComplex;
610+
return STK_IntegralComplex;
611+
}
612+
613+
llvm_unreachable("unknown scalar type");
614+
return STK_Pointer;
615+
}
616+
587617
/// \brief Determines whether the type is a C++ aggregate type or C
588618
/// aggregate or union type.
589619
///

clang/lib/Checker/GRExprEngine.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,15 +2541,19 @@ void GRExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
25412541
return;
25422542

25432543
case CK_Unknown:
2544+
case CK_Dependent:
25442545
case CK_ArrayToPointerDecay:
25452546
case CK_BitCast:
25462547
case CK_LValueBitCast:
25472548
case CK_IntegralCast:
25482549
case CK_NullToPointer:
25492550
case CK_IntegralToPointer:
25502551
case CK_PointerToIntegral:
2552+
case CK_PointerToBoolean:
2553+
case CK_IntegralToBoolean:
25512554
case CK_IntegralToFloating:
25522555
case CK_FloatingToIntegral:
2556+
case CK_FloatingToBoolean:
25532557
case CK_FloatingCast:
25542558
case CK_FloatingRealToComplex:
25552559
case CK_FloatingComplexToReal:

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,10 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
17831783
switch (E->getCastKind()) {
17841784
case CK_ToVoid:
17851785
return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1786-
1786+
1787+
case CK_Dependent:
1788+
llvm_unreachable("dependent cast kind in IR gen!");
1789+
17871790
case CK_NoOp:
17881791
if (E->getSubExpr()->Classify(getContext()).getKind()
17891792
!= Expr::Classification::CL_PRValue) {
@@ -1800,7 +1803,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
18001803
return LV;
18011804
}
18021805
// Fall through to synthesize a temporary.
1803-
1806+
18041807
case CK_Unknown:
18051808
case CK_BitCast:
18061809
case CK_ArrayToPointerDecay:
@@ -1809,10 +1812,13 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
18091812
case CK_NullToPointer:
18101813
case CK_IntegralToPointer:
18111814
case CK_PointerToIntegral:
1815+
case CK_PointerToBoolean:
18121816
case CK_VectorSplat:
18131817
case CK_IntegralCast:
1818+
case CK_IntegralToBoolean:
18141819
case CK_IntegralToFloating:
18151820
case CK_FloatingToIntegral:
1821+
case CK_FloatingToBoolean:
18161822
case CK_FloatingCast:
18171823
case CK_FloatingRealToComplex:
18181824
case CK_FloatingComplexToReal:

0 commit comments

Comments
 (0)