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
92122pub struct FunctionType {
93123 pub params : IndexVec < FunctionParamIdx , RefinedType < FunctionParamIdx > > ,
94124 pub ret : Box < RefinedType < FunctionParamIdx > > ,
125+ pub abi : FunctionAbi ,
95126}
96127
97128impl < ' 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