diff --git a/compiler/mono/src/layout.rs b/compiler/mono/src/layout.rs index 1404a55e04..70bccd2e71 100644 --- a/compiler/mono/src/layout.rs +++ b/compiler/mono/src/layout.rs @@ -78,6 +78,22 @@ impl<'a> Layout<'a> { } } + pub fn safe_to_memcpy(&self) -> bool { + use Layout::*; + + match self { + Builtin(builtin) => builtin.safe_to_memcpy(), + Struct(fields) => fields + .iter() + .all(|(_, field_layout)| field_layout.safe_to_memcpy()), + Tag(tags) => tags.iter().all(|tag_layout| tag_layout.safe_to_memcpy()), + FunctionPointer(_, _) => { + // Function pointers are immutable and can always be safely copied + true + } + } + } + pub fn stack_size(&self, pointer_size: u32) -> u32 { use Layout::*; @@ -138,6 +154,17 @@ impl<'a> Builtin<'a> { List(_) | EmptyList => Builtin::LIST_WORDS * pointer_size, } } + + pub fn safe_to_memcpy(&self) -> bool { + use Builtin::*; + + match self { + Int64 | Float64 | Bool(_, _) | Byte(_) | EmptyStr | EmptyMap | EmptyList | EmptySet => { + true + } + Str | Map(_, _) | Set(_) | List(_) => false, + } + } } #[allow(clippy::cognitive_complexity)]