Skip to content

Commit 9acc59f

Browse files
committed
Add rty::FunctionAbi and attach it to rty::FunctionType
1 parent 965b92d commit 9acc59f

2 files changed

Lines changed: 64 additions & 9 deletions

File tree

src/refine/template.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ impl<'tcx> TypeBuilder<'tcx> {
210210
registry: &'a mut R,
211211
sig: mir_ty::FnSig<'tcx>,
212212
) -> FunctionTemplateTypeBuilder<'tcx, 'a, R> {
213+
let abi = match sig.abi {
214+
rustc_target::spec::abi::Abi::Rust => rty::FunctionAbi::Rust,
215+
rustc_target::spec::abi::Abi::RustCall => rty::FunctionAbi::RustCall,
216+
_ => unimplemented!("unsupported function ABI: {:?}", sig.abi),
217+
};
213218
FunctionTemplateTypeBuilder {
214219
inner: self.clone(),
215220
registry,
@@ -225,6 +230,7 @@ impl<'tcx> TypeBuilder<'tcx> {
225230
param_rtys: Default::default(),
226231
param_refinement: None,
227232
ret_rty: None,
233+
abi,
228234
}
229235
}
230236
}
@@ -344,6 +350,7 @@ where
344350
param_rtys: Default::default(),
345351
param_refinement: None,
346352
ret_rty: None,
353+
abi: Default::default(),
347354
}
348355
.build();
349356
BasicBlockType { ty, locals }
@@ -359,6 +366,7 @@ pub struct FunctionTemplateTypeBuilder<'tcx, 'a, R> {
359366
param_refinement: Option<rty::Refinement<rty::FunctionParamIdx>>,
360367
param_rtys: HashMap<rty::FunctionParamIdx, rty::RefinedType<rty::FunctionParamIdx>>,
361368
ret_rty: Option<rty::RefinedType<rty::FunctionParamIdx>>,
369+
abi: rty::FunctionAbi,
362370
}
363371

364372
impl<'tcx, 'a, R> FunctionTemplateTypeBuilder<'tcx, 'a, R> {
@@ -478,6 +486,6 @@ where
478486
.with_scope(&builder)
479487
.build_refined(self.ret_ty)
480488
});
481-
rty::FunctionType::new(param_rtys, ret_rty)
489+
rty::FunctionType::new(param_rtys, ret_rty).with_abi(self.abi)
482490
}
483491
}

src/rty.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,36 @@ where
8383
}
8484
}
8585

86+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
87+
pub enum FunctionAbi {
88+
#[default]
89+
Rust,
90+
RustCall,
91+
}
92+
93+
impl std::fmt::Display for FunctionAbi {
94+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
95+
f.write_str(self.name())
96+
}
97+
}
98+
99+
impl FunctionAbi {
100+
pub fn name(&self) -> &'static str {
101+
match self {
102+
FunctionAbi::Rust => "rust",
103+
FunctionAbi::RustCall => "rust-call",
104+
}
105+
}
106+
107+
pub fn is_rust(&self) -> bool {
108+
matches!(self, FunctionAbi::Rust)
109+
}
110+
111+
pub fn is_rust_call(&self) -> bool {
112+
matches!(self, FunctionAbi::RustCall)
113+
}
114+
}
115+
86116
/// A function type.
87117
///
88118
/// In Thrust, function types are closed. Because of that, function types, thus its parameters and
@@ -92,6 +122,7 @@ where
92122
pub struct FunctionType {
93123
pub params: IndexVec<FunctionParamIdx, RefinedType<FunctionParamIdx>>,
94124
pub ret: Box<RefinedType<FunctionParamIdx>>,
125+
pub abi: FunctionAbi,
95126
}
96127

97128
impl<'a, 'b, D> Pretty<'a, D, termcolor::ColorSpec> for &'b FunctionType
@@ -100,15 +131,25 @@ where
100131
D::Doc: Clone,
101132
{
102133
fn pretty(self, allocator: &'a D) -> pretty::DocBuilder<'a, D, termcolor::ColorSpec> {
134+
let abi = match self.abi {
135+
FunctionAbi::Rust => allocator.nil(),
136+
abi => allocator
137+
.text("extern")
138+
.append(allocator.space())
139+
.append(allocator.as_string(abi))
140+
.append(allocator.space()),
141+
};
103142
let separator = allocator.text(",").append(allocator.line());
104-
allocator
105-
.intersperse(self.params.iter().map(|ty| ty.pretty(allocator)), separator)
106-
.parens()
107-
.append(allocator.space())
108-
.append(allocator.text("→"))
109-
.append(allocator.line())
110-
.append(self.ret.pretty(allocator))
111-
.group()
143+
abi.append(
144+
allocator
145+
.intersperse(self.params.iter().map(|ty| ty.pretty(allocator)), separator)
146+
.parens(),
147+
)
148+
.append(allocator.space())
149+
.append(allocator.text("→"))
150+
.append(allocator.line())
151+
.append(self.ret.pretty(allocator))
152+
.group()
112153
}
113154
}
114155

@@ -120,9 +161,15 @@ impl FunctionType {
120161
FunctionType {
121162
params,
122163
ret: Box::new(ret),
164+
abi: FunctionAbi::Rust,
123165
}
124166
}
125167

168+
pub fn with_abi(mut self, abi: FunctionAbi) -> Self {
169+
self.abi = abi;
170+
self
171+
}
172+
126173
/// Because function types are always closed in Thrust, we can convert this into
127174
/// [`Type<Closed>`].
128175
pub fn into_closed_ty(self) -> Type<Closed> {

0 commit comments

Comments
 (0)