mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Merge pull request #81539 from eeckstein/integer-type-apis
Swift AST/SIL: some additions and improvements of Integer type APIs
This commit is contained in:
@@ -61,8 +61,6 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
|
||||
|
||||
public var builtinVectorElementType: Type { Type(bridged: bridged.getBuiltinVectorElementType()) }
|
||||
|
||||
public var builtinFixedArrayElementType: Type { Type(bridged: bridged.getBuiltinFixedArrayElementType()) }
|
||||
|
||||
public func subst(with substitutionMap: SubstitutionMap) -> Type {
|
||||
return Type(bridged: bridged.subst(substitutionMap.bridged))
|
||||
}
|
||||
@@ -83,8 +81,6 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
|
||||
|
||||
public var builtinVectorElementType: CanonicalType { rawType.builtinVectorElementType.canonical }
|
||||
|
||||
public var builtinFixedArrayElementType: CanonicalType { rawType.builtinFixedArrayElementType.canonical }
|
||||
|
||||
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
|
||||
return rawType.subst(with: substitutionMap).canonical
|
||||
}
|
||||
@@ -195,6 +191,23 @@ extension TypeProperties {
|
||||
rawType.bridged.getRepresentationOfMetatype().representation
|
||||
}
|
||||
|
||||
public var builtinFixedArrayElementType: CanonicalType {
|
||||
CanonicalType(bridged: rawType.bridged.getBuiltinFixedArrayElementType())
|
||||
}
|
||||
public var builtinFixedArraySizeType: CanonicalType {
|
||||
CanonicalType(bridged: rawType.bridged.getBuiltinFixedArraySizeType())
|
||||
}
|
||||
|
||||
/// Returns the value of an integer value type (see `isInteger`).
|
||||
/// Returns nil if the value is not representable in an `Int`.
|
||||
public var valueOfInteger: Int? {
|
||||
let optionalInt = rawType.bridged.getValueOfIntegerType()
|
||||
if optionalInt.hasValue {
|
||||
return optionalInt.value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Assumes this is a nominal type. Returns a substitution map that sends each
|
||||
/// generic parameter of the declaration's generic signature to the corresponding
|
||||
/// generic argument of this nominal type.
|
||||
|
||||
@@ -15,7 +15,7 @@ import SIL
|
||||
extension TypeValueInst: OnoneSimplifiable, SILCombineSimplifiable {
|
||||
func simplify(_ context: SimplifyContext) {
|
||||
// If our parameter is not known statically, then bail.
|
||||
guard paramType.isInteger else {
|
||||
guard let value = value else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -862,8 +862,12 @@ final public class TypeValueInst: SingleValueInstruction, UnaryInstruction {
|
||||
CanonicalType(bridged: bridged.TypeValueInst_getParamType())
|
||||
}
|
||||
|
||||
public var value: Int {
|
||||
bridged.TypeValueInst_getValue()
|
||||
/// Returns the value of the Integer type is known and fits into an `Int`.
|
||||
public var value: Int? {
|
||||
if paramType.isInteger {
|
||||
return paramType.valueOfInteger
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3134,7 +3134,8 @@ struct BridgedASTType {
|
||||
BRIDGED_INLINE bool isBuiltinVector() const;
|
||||
BRIDGED_INLINE bool isBuiltinFixedArray() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinVectorElementType() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinFixedArrayElementType() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArrayElementType() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArraySizeType() const;
|
||||
BRIDGED_INLINE bool isBuiltinFixedWidthInteger(SwiftInt width) const;
|
||||
BRIDGED_INLINE bool isOptional() const;
|
||||
BRIDGED_INLINE bool isBuiltinType() const;
|
||||
@@ -3145,6 +3146,7 @@ struct BridgedASTType {
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getStaticTypeOfDynamicSelf() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getSuperClassType() const;
|
||||
BRIDGED_INLINE MetatypeRepresentation getRepresentationOfMetatype() const;
|
||||
BRIDGED_INLINE BridgedOptionalInt getValueOfIntegerType() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
|
||||
|
||||
@@ -550,8 +550,12 @@ BridgedASTType BridgedASTType::getBuiltinVectorElementType() const {
|
||||
return {unbridged()->castTo<swift::BuiltinVectorType>()->getElementType().getPointer()};
|
||||
}
|
||||
|
||||
BridgedASTType BridgedASTType::getBuiltinFixedArrayElementType() const {
|
||||
return {unbridged()->castTo<swift::BuiltinFixedArrayType>()->getElementType().getPointer()};
|
||||
BridgedCanType BridgedASTType::getBuiltinFixedArrayElementType() const {
|
||||
return unbridged()->castTo<swift::BuiltinFixedArrayType>()->getElementType();
|
||||
}
|
||||
|
||||
BridgedCanType BridgedASTType::getBuiltinFixedArraySizeType() const {
|
||||
return unbridged()->castTo<swift::BuiltinFixedArrayType>()->getSize();
|
||||
}
|
||||
|
||||
bool BridgedASTType::isBuiltinFixedWidthInteger(SwiftInt width) const {
|
||||
@@ -600,6 +604,10 @@ BridgedASTType::MetatypeRepresentation BridgedASTType::getRepresentationOfMetaty
|
||||
return MetatypeRepresentation(unbridged()->getAs<swift::AnyMetatypeType>()->getRepresentation());
|
||||
}
|
||||
|
||||
BridgedOptionalInt BridgedASTType::getValueOfIntegerType() const {
|
||||
return BridgedOptionalInt::getFromAPInt(unbridged()->getAs<swift::IntegerType>()->getValue());
|
||||
}
|
||||
|
||||
BridgedSubstitutionMap BridgedASTType::getContextSubstitutionMap() const {
|
||||
return unbridged()->getContextSubstitutionMap();
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
|
||||
#include "swift/Basic/SourceLoc.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/ADT/APInt.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#endif
|
||||
@@ -273,6 +274,24 @@ BRIDGED_INLINE SwiftInt BridgedOwnedString_count(BridgedOwnedString str);
|
||||
SWIFT_NAME("getter:BridgedOwnedString.isEmpty(self:)")
|
||||
BRIDGED_INLINE bool BridgedOwnedString_empty(BridgedOwnedString str);
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// MARK: BridgedOptionalInt
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct BridgedOptionalInt {
|
||||
SwiftInt value;
|
||||
bool hasValue;
|
||||
|
||||
#ifdef USED_IN_CPP_SOURCE
|
||||
static BridgedOptionalInt getFromAPInt(llvm::APInt i) {
|
||||
if (i.getSignificantBits() <= std::min(std::numeric_limits<SwiftInt>::digits, 64)) {
|
||||
return {(SwiftInt)i.getSExtValue(), true};
|
||||
}
|
||||
return {0, false};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// MARK: OStream
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@@ -699,11 +699,6 @@ struct BridgedInstruction {
|
||||
unknown
|
||||
};
|
||||
|
||||
struct OptionalInt {
|
||||
SwiftInt value;
|
||||
bool hasValue;
|
||||
};
|
||||
|
||||
enum class MarkDependenceKind {
|
||||
Unresolved, Escaping, NonEscaping
|
||||
};
|
||||
@@ -744,7 +739,7 @@ struct BridgedInstruction {
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGlobalVar GlobalAccessInst_getGlobal() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGlobalVar AllocGlobalInst_getGlobal() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedFunction FunctionRefBaseInst_getReferencedFunction() const;
|
||||
BRIDGED_INLINE OptionalInt IntegerLiteralInst_getValue() const;
|
||||
BRIDGED_INLINE BridgedOptionalInt IntegerLiteralInst_getValue() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef StringLiteralInst_getValue() const;
|
||||
BRIDGED_INLINE int StringLiteralInst_getEncoding() const;
|
||||
BRIDGED_INLINE SwiftInt TupleExtractInst_fieldIndex() const;
|
||||
@@ -862,7 +857,6 @@ struct BridgedInstruction {
|
||||
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;
|
||||
BRIDGED_INLINE bool ConvertFunctionInst_withoutActuallyEscaping() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType TypeValueInst_getParamType() const;
|
||||
BRIDGED_INLINE SwiftInt TypeValueInst_getValue() const;
|
||||
|
||||
// =========================================================================//
|
||||
// VarDeclInst and DebugVariableInst
|
||||
|
||||
@@ -1170,12 +1170,9 @@ BridgedFunction BridgedInstruction::FunctionRefBaseInst_getReferencedFunction()
|
||||
return {getAs<swift::FunctionRefBaseInst>()->getInitiallyReferencedFunction()};
|
||||
}
|
||||
|
||||
BridgedInstruction::OptionalInt BridgedInstruction::IntegerLiteralInst_getValue() const {
|
||||
BridgedOptionalInt BridgedInstruction::IntegerLiteralInst_getValue() const {
|
||||
llvm::APInt result = getAs<swift::IntegerLiteralInst>()->getValue();
|
||||
if (result.getSignificantBits() <= std::min(std::numeric_limits<SwiftInt>::digits, 64)) {
|
||||
return {(SwiftInt)result.getSExtValue(), true};
|
||||
}
|
||||
return {0, false};
|
||||
return BridgedOptionalInt::getFromAPInt(result);
|
||||
}
|
||||
|
||||
BridgedStringRef BridgedInstruction::StringLiteralInst_getValue() const {
|
||||
@@ -1662,19 +1659,6 @@ BridgedCanType BridgedInstruction::TypeValueInst_getParamType() const {
|
||||
return getAs<swift::TypeValueInst>()->getParamType();
|
||||
}
|
||||
|
||||
SwiftInt BridgedInstruction::TypeValueInst_getValue() const {
|
||||
auto tvi = getAs<swift::TypeValueInst>();
|
||||
|
||||
// Assume we've already checked that the parameter type is an IntegerType.
|
||||
auto integer = tvi->getParamType()->castTo<swift::IntegerType>();
|
||||
|
||||
if (integer->isNegative()) {
|
||||
return integer->getValue().getSExtValue();
|
||||
} else {
|
||||
return integer->getValue().getZExtValue();
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// VarDeclInst and DebugVariableInst
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
Reference in New Issue
Block a user