Skip to content

Commit 0348a5f

Browse files
Improve code readability
1 parent 096f14d commit 0348a5f

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

Diff for: src/abi.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,30 @@ impl GccType for Reg {
9696
}
9797
}
9898

99+
pub struct FnAbiGcc<'gcc> {
100+
pub return_type: Type<'gcc>,
101+
pub arguments_type: Vec<Type<'gcc>>,
102+
pub is_c_variadic: bool,
103+
pub on_stack_param_indices: FxHashSet<usize>,
104+
pub fn_attributes: Vec<FnAttribute<'gcc>>,
105+
}
106+
99107
pub trait FnAbiGccExt<'gcc, 'tcx> {
100108
// TODO(antoyo): return a function pointer type instead?
101-
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>, Vec<FnAttribute<'gcc>>);
109+
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> FnAbiGcc<'gcc>;
102110
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
103111
}
104112

105113
impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
106-
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>, Vec<FnAttribute<'gcc>>) {
114+
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> FnAbiGcc<'gcc> {
107115
let mut on_stack_param_indices = FxHashSet::default();
108116

109117
// This capacity calculation is approximate.
110118
let mut argument_tys = Vec::with_capacity(
111119
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }
112120
);
113121

114-
let return_ty =
122+
let return_type =
115123
match self.ret.mode {
116124
PassMode::Ignore => cx.type_void(),
117125
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
@@ -185,13 +193,25 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
185193
#[cfg(not(feature = "master"))]
186194
let fn_attrs = Vec::new();
187195

188-
(return_ty, argument_tys, self.c_variadic, on_stack_param_indices, fn_attrs)
196+
FnAbiGcc {
197+
return_type,
198+
arguments_type: argument_tys,
199+
is_c_variadic: self.c_variadic,
200+
on_stack_param_indices,
201+
fn_attributes: fn_attrs,
202+
}
189203
}
190204

191205
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
192-
// FIXME: Should we do something with `fn_attrs`?
193-
let (return_type, params, variadic, on_stack_param_indices, _fn_attrs) = self.gcc_type(cx);
194-
let pointer_type = cx.context.new_function_pointer_type(None, return_type, &params, variadic);
206+
// FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`?
207+
let FnAbiGcc {
208+
return_type,
209+
arguments_type,
210+
is_c_variadic,
211+
on_stack_param_indices,
212+
..
213+
} = self.gcc_type(cx);
214+
let pointer_type = cx.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic);
195215
cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);
196216
pointer_type
197217
}

Diff for: src/declare.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::ty::Ty;
66
use rustc_span::Symbol;
77
use rustc_target::abi::call::FnAbi;
88

9-
use crate::abi::FnAbiGccExt;
9+
use crate::abi::{FnAbiGcc, FnAbiGccExt};
1010
use crate::context::CodegenCx;
1111
use crate::intrinsic::llvm;
1212

@@ -80,11 +80,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
8080
}
8181

8282
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Function<'gcc> {
83-
let (return_type, params, variadic, on_stack_param_indices, fn_attrs) = fn_abi.gcc_type(self);
84-
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic);
83+
let FnAbiGcc {
84+
return_type,
85+
arguments_type,
86+
is_c_variadic,
87+
on_stack_param_indices,
88+
fn_attributes,
89+
} = fn_abi.gcc_type(self);
90+
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &arguments_type, is_c_variadic);
8591
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
86-
// We need to handle `nonnull` here where we still have access to function args.
87-
for fn_attr in fn_attrs {
92+
for fn_attr in fn_attributes {
8893
func.add_attribute(fn_attr);
8994
}
9095
func

Diff for: src/intrinsic/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, codegen: &mut
11971197
#[cfg(feature="master")]
11981198
fn gen_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, name: &str, rust_fn_sig: ty::PolyFnSig<'tcx>, codegen: &mut dyn FnMut(Builder<'a, 'gcc, 'tcx>)) -> (Type<'gcc>, Function<'gcc>) {
11991199
let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
1200-
let (typ, _, _, _, _) = fn_abi.gcc_type(cx);
1200+
let return_type = fn_abi.gcc_type(cx).return_type;
12011201
// FIXME(eddyb) find a nicer way to do this.
12021202
cx.linkage.set(FunctionType::Internal);
12031203
let func = cx.declare_fn(name, fn_abi);
@@ -1207,5 +1207,5 @@ fn gen_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, name: &str, rust_fn_sig
12071207
let block = Builder::append_block(cx, func_val, "entry-block");
12081208
let bx = Builder::build(cx, block);
12091209
codegen(bx);
1210-
(typ, func)
1210+
(return_type, func)
12111211
}

Diff for: src/type_of.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
99
use rustc_target::abi::{self, Abi, Align, F32, F64, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
1010
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
1111

12-
use crate::abi::{FnAbiGccExt, GccType};
12+
use crate::abi::{FnAbiGcc, FnAbiGccExt, GccType};
1313
use crate::context::CodegenCx;
1414
use crate::type_::struct_fields;
1515

@@ -372,8 +372,13 @@ impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
372372
}
373373

374374
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
375-
// // FIXME: Should we do something with `fn_attrs`?
376-
let (return_type, param_types, variadic, _, _fn_attrs) = fn_abi.gcc_type(self);
377-
self.context.new_function_pointer_type(None, return_type, &param_types, variadic)
375+
// FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`?
376+
let FnAbiGcc {
377+
return_type,
378+
arguments_type,
379+
is_c_variadic,
380+
..
381+
} = fn_abi.gcc_type(self);
382+
self.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic)
378383
}
379384
}

0 commit comments

Comments
 (0)