#pragma once #include "config.h" #if USE_EMBEDDED_COMPILER # include # include # include # include namespace DB { namespace ErrorCodes { extern const int LOGICAL_ERROR; } /// Returns true if type is signed, false otherwise bool typeIsSigned(const IDataType & type); /// Cast LLVM type to nullable LLVM type llvm::Type * toNullableType(llvm::IRBuilderBase & builder, llvm::Type * type); /// Returns true if type can be native LLVM type, false otherwise bool canBeNativeType(const IDataType & type); /// Returns true if type can be native LLVM type, false otherwise bool canBeNativeType(const DataTypePtr & type); template static inline bool canBeNativeType() { if constexpr (std::is_same_v || std::is_same_v) return true; else if constexpr (std::is_same_v || std::is_same_v) return true; else if constexpr (std::is_same_v || std::is_same_v) return true; else if constexpr (std::is_same_v || std::is_same_v) return true; else if constexpr (std::is_same_v || std::is_same_v) return true; return false; } /// Cast type to native LLVM type llvm::Type * toNativeType(llvm::IRBuilderBase & builder, const IDataType & type); /// Cast type to native LLVM type llvm::Type * toNativeType(llvm::IRBuilderBase & builder, const DataTypePtr & type); template static inline llvm::Type * toNativeType(llvm::IRBuilderBase & builder) { if constexpr (std::is_same_v || std::is_same_v) return builder.getInt8Ty(); else if constexpr (std::is_same_v || std::is_same_v) return builder.getInt16Ty(); else if constexpr (std::is_same_v || std::is_same_v) return builder.getInt32Ty(); else if constexpr (std::is_same_v || std::is_same_v) return builder.getInt64Ty(); else if constexpr (std::is_same_v) return builder.getFloatTy(); else if constexpr (std::is_same_v) return builder.getDoubleTy(); throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid cast to native type"); } template static inline DataTypePtr toNativeDataType() { if constexpr (std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v) return std::make_shared>(); throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid cast to native data type"); } /// Cast LLVM value with type to bool llvm::Value * nativeBoolCast(llvm::IRBuilderBase & b, const DataTypePtr & from_type, llvm::Value * value); /// Cast LLVM value with type to bool llvm::Value * nativeBoolCast(llvm::IRBuilderBase & b, const ValueWithType & value_with_type); /// Cast LLVM value with type to specified type llvm::Value * nativeCast(llvm::IRBuilderBase & b, const DataTypePtr & from_type, llvm::Value * value, const DataTypePtr & to_type); /// Cast LLVM value with type to specified type llvm::Value * nativeCast(llvm::IRBuilderBase & b, const ValueWithType & value, const DataTypePtr & to_type); template static inline llvm::Value * nativeCast(llvm::IRBuilderBase & b, llvm::Value * value, const DataTypePtr & to) { auto native_data_type = toNativeDataType(); return nativeCast(b, native_data_type, value, to); } /// Get column value for specified index as LLVM constant llvm::Constant * getColumnNativeValue(llvm::IRBuilderBase & builder, const DataTypePtr & column_type, const IColumn & column, size_t index); } #endif