Kinds of types in abstract syntax tree

I’m transpiling to C, and want to differ between value types (types that can be safely copied by C automatically) and reference types. So, imagine in the AST I have

type typ =
  | Int
  | Float
  | Struct

A struct can be both a value type and a reference type. Is it advised to add a type parameter? Even if it leads to redundancy in some cases? Or is it better to add a separate struct type?

type typ =
  | Int
  | Float
  | Struct_value
  | Struct_reference

Or

type kind =
  | Ref
  | Val

type typ =
  | Int
  | Float
  | Struct of kind

Or even

type 'a typ =
  | Int
  | Float
  | Struct of 'a

Maybe the of kind is better if other types also can be both val or ref? But you don’t have different types of kinds… :blush:

Then you have

let type_is_val = function
  | Int | Float | Struct Val -> true
  | _ -> false
1 Like

You can also have int refs or float refs, right? I’m coming from C++, so perhaps things work differently in C.

True! But I’ll limit myself to a saner subset of C. :slight_smile: Since it’s the transpiler output, not to be written or read by humans.

1 Like