use crate::{common::SymbolId, context::with_cx, ffi::FfiOption, ffi::FfiSlice};
use super::{CommonExprData, ExprPrecedence};
#[repr(C)]
#[derive(Debug)]
pub struct BoolLitExpr<'ast> {
data: CommonExprData<'ast>,
value: bool,
}
impl<'ast> BoolLitExpr<'ast> {
pub fn value(&self) -> bool {
self.value
}
}
super::impl_expr_data!(
BoolLitExpr<'ast>,
BoolLit,
fn precedence(&self) -> ExprPrecedence {
ExprPrecedence::Lit
}
);
#[cfg(feature = "driver-api")]
impl<'ast> BoolLitExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, value: bool) -> Self {
Self { data, value }
}
}
#[repr(C)]
#[derive(Debug)]
pub struct CharLitExpr<'ast> {
data: CommonExprData<'ast>,
value: char,
}
impl<'ast> CharLitExpr<'ast> {
pub fn value(&self) -> char {
self.value
}
}
super::impl_expr_data!(
CharLitExpr<'ast>,
CharLit,
fn precedence(&self) -> ExprPrecedence {
ExprPrecedence::Lit
}
);
#[cfg(feature = "driver-api")]
impl<'ast> CharLitExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, value: char) -> Self {
Self { data, value }
}
}
#[repr(C)]
#[derive(Debug)]
pub struct FloatLitExpr<'ast> {
data: CommonExprData<'ast>,
value: f64,
suffix: FfiOption<FloatSuffix>,
}
impl<'ast> FloatLitExpr<'ast> {
pub fn value(&self) -> f64 {
self.value
}
pub fn suffix(&self) -> Option<FloatSuffix> {
self.suffix.copy()
}
}
super::impl_expr_data!(
FloatLitExpr<'ast>,
FloatLit,
fn precedence(&self) -> ExprPrecedence {
ExprPrecedence::Lit
}
);
#[cfg(feature = "driver-api")]
impl<'ast> FloatLitExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, value: f64, suffix: Option<FloatSuffix>) -> Self {
Self {
data,
value,
suffix: suffix.into(),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FloatSuffix {
F32,
F64,
}
#[repr(C)]
#[derive(Debug)]
pub struct IntLitExpr<'ast> {
data: CommonExprData<'ast>,
value: u128,
suffix: FfiOption<IntSuffix>,
}
impl<'ast> IntLitExpr<'ast> {
pub fn value(&self) -> u128 {
self.value
}
pub fn suffix(&self) -> Option<IntSuffix> {
self.suffix.copy()
}
}
super::impl_expr_data!(
IntLitExpr<'ast>,
IntLit,
fn precedence(&self) -> ExprPrecedence {
ExprPrecedence::Lit
}
);
#[cfg(feature = "driver-api")]
impl<'ast> IntLitExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, value: u128, suffix: Option<IntSuffix>) -> Self {
Self {
data,
value,
suffix: suffix.into(),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IntSuffix {
Isize,
I8,
I16,
I32,
I64,
I128,
Usize,
U8,
U16,
U32,
U64,
U128,
}
#[repr(C)]
#[derive(Debug)]
pub struct StrLitExpr<'ast> {
data: CommonExprData<'ast>,
is_raw: bool,
str_data: StrLitData<'ast>,
}
impl<'ast> StrLitExpr<'ast> {
pub fn is_raw_lit(&self) -> bool {
self.is_raw
}
pub fn is_standard_lit(&self) -> bool {
!self.is_raw
}
pub fn is_byte_str(&self) -> bool {
matches!(self.str_data, StrLitData::Bytes(_))
}
pub fn str_value(&self) -> Option<&str> {
match &self.str_data {
StrLitData::Sym(sym) => Some(with_cx(self, |cx| cx.symbol_str(*sym))),
StrLitData::Bytes(bytes) => std::str::from_utf8(bytes.get()).ok(),
}
}
pub fn byte_value(&self) -> &[u8] {
match &self.str_data {
StrLitData::Sym(sym) => with_cx(self, |cx| cx.symbol_str(*sym)).as_bytes(),
StrLitData::Bytes(bytes) => bytes.get(),
}
}
}
super::impl_expr_data!(
StrLitExpr<'ast>,
StrLit,
fn precedence(&self) -> ExprPrecedence {
ExprPrecedence::Lit
}
);
#[cfg(feature = "driver-api")]
impl<'ast> StrLitExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, is_raw: bool, str_data: StrLitData<'ast>) -> Self {
Self { data, is_raw, str_data }
}
}
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
enum StrLitData<'ast> {
Sym(SymbolId),
Bytes(FfiSlice<'ast, u8>),
}