Skip to content

Commit 5f1cecd

Browse files
authored
transpile: Ensure that there are always types for primitive type kinds (#1865)
This allows code elsewhere to rely on these kinds always existing, for calling `type_for_kind` and the like. This can be expanded to include more types in the future if needed.
2 parents 389baea + 5684416 commit 5f1cecd

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,28 @@ impl ConversionContext {
503503
self.visit_node(untyped_context, node_id, new_id, expected_ty)
504504
}
505505

506+
// Check what primitive kinds were emitted by the compiler.
507+
let mut found_kinds: HashMap<_, _> = CTypeKind::PRIMITIVE_KINDS
508+
.into_iter()
509+
.map(|kind| (kind, false))
510+
.collect();
511+
512+
for Located { kind, .. } in self.typed_context.c_types.values() {
513+
if let Some(is_found) = found_kinds.get_mut(kind) {
514+
*is_found = true;
515+
}
516+
}
517+
518+
// If any primitives are missing, add them ourselves.
519+
for (kind, is_found) in found_kinds {
520+
if !is_found {
521+
let new_id = self.id_mapper.fresh_id();
522+
self.add_type(new_id, not_located(kind));
523+
self.processed_nodes
524+
.insert(new_id, self::node_types::OTHER_TYPE);
525+
}
526+
}
527+
506528
// Function declarations' types look through typedefs, but we want to use the types with
507529
// typedefs intact in some cases during translation. To ensure that these types exist in the
508530
// `TypedAstContext`, iterate over all function decls, compute their adjusted type using

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ pub struct AsmOperand {
24832483
}
24842484

24852485
/// Type qualifiers (6.7.3)
2486-
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
2486+
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)]
24872487
pub struct Qualifiers {
24882488
/// The `const` qualifier, which marks lvalues as non-assignable.
24892489
///
@@ -2519,7 +2519,7 @@ impl Qualifiers {
25192519
}
25202520

25212521
/// Qualified type
2522-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2522+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
25232523
pub struct CQualTypeId {
25242524
pub qualifiers: Qualifiers,
25252525
pub ctype: CTypeId,
@@ -2546,7 +2546,7 @@ impl CQualTypeId {
25462546
/// Represents a type in C (6.2.5 Types)
25472547
///
25482548
/// Reflects the types in <http://clang.llvm.org/doxygen/classclang_1_1Type.html>
2549-
#[derive(Debug, Clone, PartialEq, Eq)]
2549+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25502550
pub enum CTypeKind {
25512551
Void,
25522552

@@ -2670,6 +2670,17 @@ pub enum CTypeKind {
26702670
}
26712671

26722672
impl CTypeKind {
2673+
/// Kinds for C primitive types. These are emitted by the compiler, but possibly only if
2674+
/// they are actually used in the code.
2675+
pub const PRIMITIVE_KINDS: [CTypeKind; 16] = {
2676+
use CTypeKind::*;
2677+
[
2678+
Void, Bool, Char, SChar, Short, Int, Long, LongLong, UChar, UShort, UInt, ULong,
2679+
ULongLong, Float, Double, LongDouble,
2680+
]
2681+
};
2682+
2683+
/// Kinds for Rust types that are pulled back into C, for more fine-grained translation.
26732684
pub const PULLBACK_KINDS: [CTypeKind; 16] = {
26742685
use CTypeKind::*;
26752686
[

0 commit comments

Comments
 (0)