Skip to main content

Type

Enum Type 

Source
#[non_exhaustive]
pub enum Type {
Show 13 variants Bool, I8, I16, I32, I64, U8, U16, U32, U64, F32, F64, Ptr, Ref,
}
Expand description

The type of an SSA value.

Integers are sized and carry their signedness, mirroring the value domains of the operation semantics (specs/OPS.md §1): every signedness-dependent operation (overflow detection, division, right shift, ordering, conversions) takes its signedness from its operand type, so an instruction cannot disagree with the domain it computes in. Ptr is an untraced address; Ref is a garbage-collected reference that a moving collector may relocate at a safepoint (see the safepoint rules in specs/IR.md §9).

Aggregates are memory types only (MemType): they live in stack slots and globals and are reached through addresses, never held in an SSA value. Vector types are reserved; the enum is #[non_exhaustive] so they can be added in a minor release.

§Examples

use ir_lang::{PointerWidth, Type};

assert!(Type::I32.is_signed_int());
assert!(Type::U8.is_unsigned_int());
assert_eq!(Type::I16.int_bits(), Some(16));
assert_eq!(Type::Ptr.size(PointerWidth::P64), 8);
assert_eq!(Type::F32.to_string(), "f32");

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Bool

A boolean: false or true (one byte in memory, 0 or 1).

§

I8

A signed 8-bit integer.

§

I16

A signed 16-bit integer.

§

I32

A signed 32-bit integer.

§

I64

A signed 64-bit integer.

§

U8

An unsigned 8-bit integer.

§

U16

An unsigned 16-bit integer.

§

U32

An unsigned 32-bit integer.

§

U64

An unsigned 64-bit integer.

§

F32

An IEEE 754 binary32 float.

§

F64

An IEEE 754 binary64 float.

§

Ptr

An untraced address, as wide as the module’s PointerWidth.

§

Ref

A garbage-collected reference, as wide as the module’s PointerWidth.

Implementations§

Source§

impl Type

Source

pub const ALL: [Type; 13]

Every value type, in a fixed order (useful for generators and tables).

Source

pub const fn is_int(self) -> bool

Whether this is an integer type (signed or unsigned).

§Examples
use ir_lang::Type;

assert!(Type::U64.is_int());
assert!(!Type::Bool.is_int());
Source

pub const fn is_signed_int(self) -> bool

Whether this is a signed integer type.

§Examples
use ir_lang::Type;

assert!(Type::I8.is_signed_int());
assert!(!Type::U8.is_signed_int());
Source

pub const fn is_unsigned_int(self) -> bool

Whether this is an unsigned integer type.

§Examples
use ir_lang::Type;

assert!(Type::U16.is_unsigned_int());
assert!(!Type::I16.is_unsigned_int());
Source

pub const fn is_float(self) -> bool

Whether this is a float type.

§Examples
use ir_lang::Type;

assert!(Type::F64.is_float());
assert!(!Type::I64.is_float());
Source

pub const fn is_numeric(self) -> bool

Whether this is an integer or float type.

§Examples
use ir_lang::Type;

assert!(Type::F32.is_numeric());
assert!(!Type::Ptr.is_numeric());
Source

pub const fn int_bits(self) -> Option<u32>

The width of an integer type in bits, or None for other types.

§Examples
use ir_lang::Type;

assert_eq!(Type::U32.int_bits(), Some(32));
assert_eq!(Type::F32.int_bits(), None);
Source

pub const fn bits(self, ptr: PointerWidth) -> u32

The width of the type’s value in bits, for the given pointer width (Bool is 1 bit).

§Examples
use ir_lang::{PointerWidth, Type};

assert_eq!(Type::Bool.bits(PointerWidth::P64), 1);
assert_eq!(Type::Ref.bits(PointerWidth::P32), 32);
Source

pub const fn size(self, ptr: PointerWidth) -> u32

The size of the type in memory, in bytes (also its natural alignment).

§Examples
use ir_lang::{PointerWidth, Type};

assert_eq!(Type::Bool.size(PointerWidth::P64), 1);
assert_eq!(Type::I16.size(PointerWidth::P64), 2);
assert_eq!(Type::Ptr.size(PointerWidth::P32), 4);
Source

pub const fn int(bits: u32, signed: bool) -> Option<Type>

The integer type of the given width and signedness, if there is one.

§Examples
use ir_lang::Type;

assert_eq!(Type::int(32, true), Some(Type::I32));
assert_eq!(Type::int(12, false), None);
Source

pub const fn name(self) -> &'static str

The textual name of the type (i32, ptr, …).

§Examples
use ir_lang::Type;

assert_eq!(Type::Ref.name(), "ref");
Source

pub fn from_name(name: &str) -> Option<Type>

Looks a type up by its textual name.

§Examples
use ir_lang::Type;

assert_eq!(Type::from_name("u16"), Some(Type::U16));
assert_eq!(Type::from_name("int"), None);

Trait Implementations§

Source§

impl Clone for Type

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Type

Source§

impl Debug for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Type

Source§

impl From<Type> for MemType

Source§

fn from(ty: Type) -> Self

Converts to this type from the input type.
Source§

impl Hash for Type

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Type

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Type

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Type

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Type

Auto Trait Implementations§

§

impl Freeze for Type

§

impl RefUnwindSafe for Type

§

impl Send for Type

§

impl Sync for Type

§

impl Unpin for Type

§

impl UnsafeUnpin for Type

§

impl UnwindSafe for Type

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.