Skip to content

Commit 768e9d2

Browse files
committed
[Rust] Restructure type APIs into types module
This helps with documentation, giving a single module for those working with types to find related APIs Also split out enumeration and structure APIs into their own file, since they have their own backing data separate from `Type`.
1 parent 7c0484d commit 768e9d2

23 files changed

+980
-952
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ rust/examples/*/target/
9090
rust/examples/dwarf/*/target/
9191
# Allow the test binaries
9292
!/rust/fixtures/bin/**
93+
!/rust/src/types
9394

9495
# Debugger docs
9596
docs/img/debugger

rust/examples/dump_type_library.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use binaryninja::binary_view::BinaryView;
44
use binaryninja::file_metadata::FileMetadata;
5-
use binaryninja::type_library::TypeLibrary;
6-
use binaryninja::type_printer::{CoreTypePrinter, TokenEscapingType};
5+
use binaryninja::types::library::TypeLibrary;
6+
use binaryninja::types::printer::{CoreTypePrinter, TokenEscapingType};
77

88
fn main() {
99
let type_lib_str = std::env::args().nth(1).expect("No type library provided");

rust/examples/type_printer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use binaryninja::type_printer::{CoreTypePrinter, TokenEscapingType};
1+
use binaryninja::types::printer::{CoreTypePrinter, TokenEscapingType};
22
use binaryninja::types::{MemberAccess, MemberScope, Structure, StructureMember, Type};
33

44
fn main() {

rust/src/binary_view.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ pub use crate::workflow::AnalysisContext;
3030
use crate::architecture::{Architecture, CoreArchitecture};
3131
use crate::base_detection::BaseAddressDetection;
3232
use crate::basic_block::BasicBlock;
33+
use crate::binary_view::search::SearchQuery;
3334
use crate::component::Component;
3435
use crate::confidence::Conf;
3536
use crate::data_buffer::DataBuffer;
3637
use crate::debuginfo::DebugInfo;
38+
use crate::disassembly::DisassemblySettings;
3739
use crate::external_library::{ExternalLibrary, ExternalLocation};
3840
use crate::file_accessor::{Accessor, FileAccessor};
3941
use crate::file_metadata::FileMetadata;
@@ -53,12 +55,12 @@ use crate::settings::Settings;
5355
use crate::string::*;
5456
use crate::symbol::{Symbol, SymbolType};
5557
use crate::tags::{Tag, TagType};
56-
use crate::type_container::TypeContainer;
57-
use crate::type_library::TypeLibrary;
5858
use crate::types::{
5959
NamedTypeReference, QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type,
60+
TypeArchive, TypeArchiveId, TypeContainer, TypeLibrary,
6061
};
6162
use crate::variable::DataVariable;
63+
use crate::workflow::Workflow;
6264
use crate::{Endianness, BN_FULL_CONFIDENCE};
6365
use std::collections::HashMap;
6466
use std::ffi::{c_char, c_void, CString};
@@ -67,17 +69,12 @@ use std::ops::Range;
6769
use std::path::{Path, PathBuf};
6870
use std::ptr::NonNull;
6971
use std::{result, slice};
70-
// TODO : general reorg of modules related to bv
7172

7273
pub mod memory_map;
7374
pub mod reader;
7475
pub mod search;
7576
pub mod writer;
7677

77-
use crate::binary_view::search::SearchQuery;
78-
use crate::disassembly::DisassemblySettings;
79-
use crate::type_archive::{TypeArchive, TypeArchiveId};
80-
use crate::workflow::Workflow;
8178
pub use memory_map::MemoryMap;
8279
pub use reader::BinaryReader;
8380
pub use writer::BinaryWriter;

rust/src/collaboration/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::progress::{NoProgressCallback, ProgressCallback};
1313
use crate::project::file::ProjectFile;
1414
use crate::rc::Ref;
1515
use crate::string::{raw_to_string, BnString, IntoCStr};
16-
use crate::type_archive::{TypeArchive, TypeArchiveMergeConflict};
16+
use crate::types::archive::{TypeArchive, TypeArchiveMergeConflict};
1717

1818
/// Get the default directory path for a remote Project. This is based off the Setting for
1919
/// collaboration.directory, the project's id, and the project's remote's id.

rust/src/component.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::binary_view::{BinaryView, BinaryViewExt};
22
use crate::function::Function;
33
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
44
use crate::string::{BnString, IntoCStr};
5-
use crate::types::ComponentReferencedType;
5+
use crate::types::Type;
66
use std::ffi::c_char;
77
use std::fmt::Debug;
88
use std::ptr::NonNull;
@@ -304,3 +304,24 @@ unsafe impl CoreArrayProviderInner for Component {
304304
Guard::new(Self::from_raw(raw_ptr), context)
305305
}
306306
}
307+
308+
// TODO: Remove this struct, or make it not a ZST with a terrible array provider.
309+
/// ZST used only for `Array<ComponentReferencedType>`.
310+
pub struct ComponentReferencedType;
311+
312+
impl CoreArrayProvider for ComponentReferencedType {
313+
type Raw = *mut BNType;
314+
type Context = ();
315+
type Wrapped<'a> = &'a Type;
316+
}
317+
318+
unsafe impl CoreArrayProviderInner for ComponentReferencedType {
319+
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
320+
BNComponentFreeReferencedTypes(raw, count)
321+
}
322+
323+
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
324+
// SAFETY: &*mut BNType == &Type (*mut BNType == Type)
325+
std::mem::transmute(raw)
326+
}
327+
}

rust/src/data_notification.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use crate::section::Section;
1515
use crate::segment::Segment;
1616
use crate::symbol::Symbol;
1717
use crate::tags::{TagReference, TagType};
18-
use crate::type_archive::TypeArchive;
19-
use crate::types::{QualifiedName, Type};
18+
use crate::types::{QualifiedName, Type, TypeArchive};
2019
use crate::variable::DataVariable;
2120

2221
macro_rules! trait_handler {

rust/src/language_representation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use crate::high_level_il::{HighLevelExpressionIndex, HighLevelILFunction};
1414
use crate::line_formatter::CoreLineFormatter;
1515
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref, RefCountable};
1616
use crate::string::{BnString, IntoCStr};
17-
use crate::type_parser::CoreTypeParser;
18-
use crate::type_printer::CoreTypePrinter;
17+
use crate::types::{CoreTypeParser, CoreTypePrinter};
1918

2019
pub type InstructionTextTokenContext = BNInstructionTextTokenContext;
2120
pub type ScopeType = BNScopeType;

rust/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ pub mod string;
8181
pub mod symbol;
8282
pub mod tags;
8383
pub mod template_simplifier;
84-
pub mod type_archive;
85-
pub mod type_container;
86-
pub mod type_library;
87-
pub mod type_parser;
88-
pub mod type_printer;
8984
pub mod types;
9085
pub mod update;
9186
pub mod variable;

rust/src/platform.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
//! Contains all information related to the execution environment of the binary, mainly the calling conventions used
1616
17-
use crate::type_container::TypeContainer;
18-
use crate::type_parser::{TypeParserError, TypeParserErrorSeverity, TypeParserResult};
1917
use crate::{
2018
architecture::{Architecture, CoreArchitecture},
2119
calling_convention::CoreCallingConvention,
2220
rc::*,
2321
string::*,
24-
type_library::TypeLibrary,
25-
types::QualifiedNameAndType,
22+
types::{
23+
QualifiedNameAndType, TypeContainer, TypeLibrary, TypeParserError, TypeParserErrorSeverity,
24+
TypeParserResult,
25+
},
2626
};
2727
use binaryninjacore_sys::*;
2828
use std::fmt::Debug;

0 commit comments

Comments
 (0)