Add ty_fn_ptr function to create function pointer type

This commit is contained in:
Giga Bowser 2024-10-24 17:46:14 -04:00
parent 3f48da27dd
commit a87e1aad81

View file

@ -15,7 +15,11 @@ use parser::{Edition, T};
use rowan::NodeOrToken;
use stdx::{format_to, format_to_acc, never};
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
use crate::{
ast::{self, Param},
utils::is_raw_identifier,
AstNode, SourceFile, SyntaxKind, SyntaxToken,
};
/// While the parent module defines basic atomic "constructors", the `ext`
/// module defines shortcuts for common things.
@ -198,6 +202,38 @@ pub fn ty_alias(
ast_from_text(&s)
}
pub fn ty_fn_ptr<I: Iterator<Item = Param>>(
for_lifetime_list: Option<ast::GenericParamList>,
is_unsafe: bool,
abi: Option<ast::Abi>,
params: I,
ret_type: Option<ast::RetType>,
) -> ast::FnPtrType {
let mut s = String::from("type __ = ");
if let Some(list) = for_lifetime_list {
format_to!(s, "for{} ", list);
}
if is_unsafe {
s.push_str("unsafe ");
}
if let Some(abi) = abi {
format_to!(s, "{} ", abi)
}
s.push_str("fn");
format_to!(s, "({})", params.map(|p| p.to_string()).join(", "));
if let Some(ret_type) = ret_type {
format_to!(s, " {}", ret_type);
}
ast_from_text(&s)
}
pub fn assoc_item_list() -> ast::AssocItemList {
ast_from_text("impl C for D {}")
}
@ -862,6 +898,10 @@ pub fn item_const(
ast_from_text(&format!("{visibility} const {name}: {ty} = {expr};"))
}
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
ast_from_text(&format!("fn f({ty}) {{ }}"))
}
pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
ast_from_text(&format!("fn f({pat}: {ty}) {{ }}"))
}