Unsafe pointer

This commit is contained in:
2026-06-28 05:24:30 +02:00
parent bf5d243036
commit 3ddb1bc601
6 changed files with 44 additions and 68 deletions
+7 -14
View File
@@ -176,7 +176,7 @@ impl GetMist for TypeExpr {
} }
Self::UnsafePtr { mutable, ty } => { Self::UnsafePtr { mutable, ty } => {
let mutable = if *mutable { "mut " } else { "const " }; let mutable = if *mutable { "mut " } else { "const " };
format!("*{mutable}{}", ty.get_mist()) format!("{} {mutable} unsafe&", ty.get_mist())
} }
Self::Ref { Self::Ref {
lifetime, lifetime,
@@ -185,19 +185,12 @@ impl GetMist for TypeExpr {
} => { } => {
let base = ty.get_mist(); let base = ty.get_mist();
if let Some(lifetime) = lifetime { if let Some(lifetime) = lifetime {
match lifetime { format!(
Lifetime::Lifetime(v) => { "{} {} '{}&",
format!( base,
"{} {} '{}&", if *mutable { "mut" } else { "" },
base, lifetime.get_mist()
if *mutable { "mut" } else { "" }, )
v.get_mist()
)
}
Lifetime::Unsafe => {
format!("{} {} unsafe&", base, if *mutable { "mut" } else { "const" })
}
}
} else if *mutable { } else if *mutable {
format!("{} mut&", base) format!("{} mut&", base)
} else { } else {
+7 -17
View File
@@ -239,6 +239,7 @@ impl GetRust for TypeExpr {
Self::UnsafePtr { mutable, ty } => { Self::UnsafePtr { mutable, ty } => {
let mutable = if *mutable { "mut " } else { "const " }; let mutable = if *mutable { "mut " } else { "const " };
format!("*{mutable}{}", ty.get_rust()) format!("*{mutable}{}", ty.get_rust())
} }
Self::Ref { Self::Ref {
@@ -247,23 +248,12 @@ impl GetRust for TypeExpr {
ty, ty,
} => { } => {
if let Some(lifetime) = lifetime { if let Some(lifetime) = lifetime {
match lifetime { format!(
Lifetime::Lifetime(v) => { "&'{} {}{}",
format!( lifetime.get_rust(),
"&'{} {}{}", if *mutable { "mut " } else { "" },
v.get_rust(), ty.get_rust()
if *mutable { "mut " } else { "" }, )
ty.get_rust()
)
}
Lifetime::Unsafe => {
format!(
"*{} {}",
if *mutable { "mut" } else { "const" },
ty.get_rust()
)
}
}
} else { } else {
format!("&{}{}", if *mutable { "mut " } else { "" }, ty.get_rust()) format!("&{}{}", if *mutable { "mut " } else { "" }, ty.get_rust())
} }
+1 -7
View File
@@ -17,16 +17,10 @@ pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>); pub struct ParamList(pub Vec<VarDecl>);
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum Lifetime {
Lifetime(Identifier),
Unsafe,
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum TypeExpr { pub enum TypeExpr {
Ref { Ref {
lifetime: Option<Lifetime>, lifetime: Option<Identifier>,
mutable: bool, mutable: bool,
ty: Box<TypeExpr>, ty: Box<TypeExpr>,
}, },
+6 -5
View File
@@ -81,7 +81,6 @@ module_program = {
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
mutable = { "mut" } mutable = { "mut" }
unsafe_kw = { "unsafe" }
// ====================================================== // ======================================================
// GENERICS // GENERICS
@@ -104,10 +103,12 @@ path_type = {
static_path ~ generics? static_path ~ generics?
} }
ref_lifetime = { lifetime | unsafe_kw } unsafe_ref_type = {
mutable? ~ "unsafe" ~ "*"
}
ref_type = { ref_type = {
mutable? ~ ref_lifetime? ~ "&" mutable? ~ lifetime? ~ "&"
} }
dyn_type = { dyn_type = {
@@ -115,7 +116,7 @@ dyn_type = {
} }
type_expr = { type_expr = {
(path_type | tuple_type | dyn_type) ~ ref_type* (path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type)*
} }
lifetime = { lifetime = {
@@ -467,7 +468,7 @@ match_stmt = {
closure = { closure = {
("(" ~ param_list ~ ")" ~ "=>" ~ expr) ("(" ~ param_list ~ ")" ~ "=>" ~ expr)
| (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr) | (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr)
} }
// ------------------------------------------------------ // ------------------------------------------------------
+22 -9
View File
@@ -21,16 +21,29 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
let mut ty = TypeExpr::try_from(inner.next().unwrap())?; let mut ty = TypeExpr::try_from(inner.next().unwrap())?;
for ref_pair in inner { for ref_pair in inner {
let mut ref_inner = ref_pair.into_inner(); let mut ref_inner = ref_pair.clone().into_inner();
ty = TypeExpr::Ref { match ref_pair.as_rule() {
lifetime: consume_rule(&mut ref_inner, Rule::ref_lifetime) Rule::ref_type => {
.map(|v| v.into_inner().next().map(Lifetime::try_from)) ty = TypeExpr::Ref {
.unwrap_or_default() mutable: listen_rule(&mut ref_inner, Rule::mutable),
.transpose()?, lifetime: consume_rule(&mut ref_inner, Rule::lifetime)
mutable: listen_rule(&mut ref_inner, Rule::mutable), .map(|v| v.into_inner().next().map(Identifier::try_from))
ty: Box::new(ty), .unwrap_or_default()
}; .transpose()?,
ty: Box::new(ty),
};
}
Rule::unsafe_ref_type => {
ty = TypeExpr::UnsafePtr {
mutable: listen_rule(&mut ref_inner, Rule::mutable),
ty: Box::new(ty),
};
}
_ => AstError::bug_unimplemented(ref_pair)?,
}
} }
Ok(ty) Ok(ty)
+1 -16
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_ensure, ast_ensure,
error::{self, AstError, AstResult}, error::{AstError, AstResult},
parser::{consume_rule, consume_rule_map, listen_rule}, parser::{consume_rule, consume_rule_map, listen_rule},
}; };
@@ -66,18 +66,3 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Override {
}) })
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Lifetime {
type Error = AstError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
match pair.as_rule() {
Rule::ref_lifetime => inner.next().unwrap().try_into(),
Rule::lifetime => Ok(Lifetime::Lifetime(inner.next().unwrap().try_into()?)),
Rule::unsafe_kw => Ok(Lifetime::Unsafe),
_ => error::AstError::bug_unimplemented(pair),
}
}
}