Auto merge of #141942 - ShoyuVanilla:smir-repr, r=oli-obk · rust-lang/rust@0a39445 (original) (raw)

`@@ -21,10 +21,13 @@ use stable_mir::abi::{

`

21

21

`ArgAbi, CallConvention, FieldsShape, IntegerLength, PassMode, Primitive, Scalar, ValueAbi,

`

22

22

`VariantsShape,

`

23

23

`};

`

``

24

`+

use stable_mir::mir::MirVisitor;

`

24

25

`use stable_mir::mir::mono::Instance;

`

25

26

`use stable_mir:🎯:MachineInfo;

`

``

27

`+

use stable_mir::ty::{AdtDef, RigidTy, Ty, TyKind};

`

26

28

`use stable_mir::{CrateDef, CrateItem, CrateItems, ItemKind};

`

27

29

`use std::assert_matches::assert_matches;

`

``

30

`+

use std::collections::HashSet;

`

28

31

`use std::convert::TryFrom;

`

29

32

`use std::io::Write;

`

30

33

`use std::ops::ControlFlow;

`

`@@ -67,6 +70,17 @@ fn test_stable_mir() -> ControlFlow<()> {

`

67

70

`assert!(ptr_variadic_fn_abi.c_variadic);

`

68

71

`assert_eq!(ptr_variadic_fn_abi.args.len(), 1);

`

69

72

``

``

73

`+

let entry = stable_mir::entry_fn().unwrap();

`

``

74

`+

let main_fn = Instance::try_from(entry).unwrap();

`

``

75

`+

let mut visitor = AdtDefVisitor::default();

`

``

76

`+

visitor.visit_body(&main_fn.body().unwrap());

`

``

77

`+

let AdtDefVisitor { adt_defs } = visitor;

`

``

78

`+

assert_eq!(adt_defs.len(), 1);

`

``

79

+

``

80

`+

// Test ADT representation options

`

``

81

`+

let repr_c_struct = adt_defs.iter().find(|def| def.trimmed_name() == "ReprCStruct").unwrap();

`

``

82

`+

assert!(repr_c_struct.repr().flags.is_c);

`

``

83

+

70

84

`ControlFlow::Continue(())

`

71

85

`}

`

72

86

``

`@@ -138,6 +152,20 @@ fn get_item<'a>(

`

138

152

` items.iter().find(|crate_item| (item.0 == crate_item.kind()) && crate_item.name() == item.1)

`

139

153

`}

`

140

154

``

``

155

`+

#[derive(Default)]

`

``

156

`+

struct AdtDefVisitor {

`

``

157

`+

adt_defs: HashSet,

`

``

158

`+

}

`

``

159

+

``

160

`+

impl MirVisitor for AdtDefVisitor {

`

``

161

`+

fn visit_ty(&mut self, ty: &Ty, _location: stable_mir::mir::visit::Location) {

`

``

162

`+

if let TyKind::RigidTy(RigidTy::Adt(adt, _)) = ty.kind() {

`

``

163

`+

self.adt_defs.insert(adt);

`

``

164

`+

}

`

``

165

`+

self.super_ty(ty)

`

``

166

`+

}

`

``

167

`+

}

`

``

168

+

141

169

`/// This test will generate and analyze a dummy crate using the stable mir.

`

142

170

`/// For that, it will first write the dummy crate into a file.

`

143

171

`` /// Then it will create a StableMir using custom arguments and then

``

`@@ -147,7 +175,7 @@ fn main() {

`

147

175

`generate_input(&path).unwrap();

`

148

176

`let args = &[

`

149

177

`"rustc".to_string(),

`

150

``

`-

"--crate-type=lib".to_string(),

`

``

178

`+

"-Cpanic=abort".to_string(),

`

151

179

`"--crate-name".to_string(),

`

152

180

`CRATE_NAME.to_string(),

`

153

181

` path.to_string(),

`

`@@ -185,6 +213,13 @@ fn generate_input(path: &str) -> std::io::Result<()> {

`

185

213

` // We only care about the signature.

`

186

214

` todo!()

`

187

215

` }}

`

``

216

+

``

217

`+

fn main() {{

`

``

218

`+

#[repr(C)]

`

``

219

`+

struct ReprCStruct;

`

``

220

+

``

221

`+

let _s = ReprCStruct;

`

``

222

`+

}}

`

188

223

` "#

`

189

224

`)?;

`

190

225

`Ok(())

`