Offset of 'tag' of a union as a constant number?

Is there a way to get the offset and size of the ‘tag’ of a union as a constant number? Need it for some domain-specific codegen.

I know about offset_of procedure, but I’m unsure how to use it for this specific case.

import "base:intrinsics"

offset :: intrinsics.type_union_tag_offset(T)
size :: size_of(intrinsics.type_union_tag_type(T))

should do it!

1 Like

Yep works. Thanks a lot.

actually on that topic, is there some trick to casting a variant constant to a union field type constant? I’m facing some issues:

ReflectEntry :: struct {
	// some fields...
	variant: ReflectEntryVariant,
}

ReflectEntryVariant :: union {
	ReflectEntryU8,
	ReflectEntryI8,
	// more variants...
}

ReflectEntryU8 :: distinct ReflectEntryInteger
ReflectEntryI8 :: distinct ReflectEntryInteger
ReflectEntryInteger :: struct { }

MY_CONST2: ReflectEntryVariant : ReflectEntryI8{} // compiles fine
mv: ReflectEntry : ReflectEntry{name = "cool", offset = 48, /* more */ } // compiles fine
mv: ReflectEntry : ReflectEntry{variant = MY_CONST2} // doesn't compile <--- problem

found the reason on github

paraphrasing:

This is partially due to the LLVM type system not being able to represent unions arbitrarily in the IR without having rely on loads of bodges.
So this partially why they are not allowed as valid constants.

suxx

1 Like