|
| 1 | +// This code is part of Qiskit. |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2026 |
| 4 | +// |
| 5 | +// This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +// obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +// of this source tree or at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// Any modifications or derivative works of this code must retain this |
| 10 | +// copyright notice, and modified files need to carry a notice indicating |
| 11 | +// that they have been altered from the originals. |
| 12 | + |
| 13 | +use cbindgen::bindgen::ir; |
| 14 | +use hashbrown::HashMap; |
| 15 | + |
| 16 | +/// Render a given type object into a string representing it in C. |
| 17 | +fn render_type(ty: &ir::Type, config: &cbindgen::Config) -> String { |
| 18 | + fn render(ty: &ir::Type, config: &cbindgen::Config, acc: &mut String) { |
| 19 | + match ty { |
| 20 | + ir::Type::Ptr { |
| 21 | + ty, |
| 22 | + is_const, |
| 23 | + is_nullable: _, |
| 24 | + is_ref, |
| 25 | + } => { |
| 26 | + assert!(!is_ref, "C++ reference-likes not handled"); |
| 27 | + if *is_const { |
| 28 | + acc.push_str("const "); |
| 29 | + } |
| 30 | + render(ty, config, acc); |
| 31 | + acc.push_str(" *"); |
| 32 | + } |
| 33 | + ir::Type::Path(p) => acc.push_str(p.export_name()), |
| 34 | + ir::Type::Primitive(ty) => acc.push_str(ty.to_repr_c(config)), |
| 35 | + ir::Type::Array(..) => todo!("array types not yet handled"), |
| 36 | + ir::Type::FuncPtr { |
| 37 | + args, |
| 38 | + ret, |
| 39 | + is_nullable, |
| 40 | + never_return, |
| 41 | + } => { |
| 42 | + assert!(!is_nullable, "nullability of funcptrs is not handled"); |
| 43 | + assert!(!never_return, "diverging functions not handled"); |
| 44 | + render(ret, config, acc); |
| 45 | + acc.push_str("(*)("); |
| 46 | + let mut args = args.iter(); |
| 47 | + if let Some((_, first)) = args.next() { |
| 48 | + render(first, config, acc); |
| 49 | + for (_, arg) in args { |
| 50 | + acc.push_str(", "); |
| 51 | + render(arg, config, acc) |
| 52 | + } |
| 53 | + } |
| 54 | + acc.push(')'); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + let mut acc = String::new(); |
| 59 | + render(ty, config, &mut acc); |
| 60 | + acc |
| 61 | +} |
| 62 | + |
| 63 | +/// Calculate a mapping of exported function names to C casts to appropriate function-pointer types. |
| 64 | +pub fn functions_as_funcptr_casts(bindings: &cbindgen::Bindings) -> HashMap<&str, String> { |
| 65 | + let to_funcptr = |func: &ir::Function| { |
| 66 | + let to_funcptr_arg = |arg: &ir::FunctionArgument| { |
| 67 | + let ir::FunctionArgument { |
| 68 | + name: _, |
| 69 | + ty, |
| 70 | + array_length, |
| 71 | + } = arg; |
| 72 | + assert!(array_length.is_none(), "array arguments not handled"); |
| 73 | + (None, ty.clone()) |
| 74 | + }; |
| 75 | + ir::Type::FuncPtr { |
| 76 | + ret: Box::new(func.ret.clone()), |
| 77 | + args: func.args.iter().map(to_funcptr_arg).collect(), |
| 78 | + is_nullable: false, |
| 79 | + never_return: false, |
| 80 | + } |
| 81 | + }; |
| 82 | + let config = &bindings.config; |
| 83 | + bindings |
| 84 | + .functions |
| 85 | + .iter() |
| 86 | + .map(|func| { |
| 87 | + let funcptr = to_funcptr(func); |
| 88 | + (func.path.name(), render_type(&funcptr, config)) |
| 89 | + }) |
| 90 | + .collect() |
| 91 | +} |
0 commit comments