Structs support almost complete

Hello everyone.

I’m very happy to announce that structs support is almost complete. This includes arbitrary struct definition order in the source code (i.e. - a struct may reference a struct that is only defined further down the file). This also includes circular dependency detection, which results in an error message with reasonable compilation times.

Code speaks louder than words, so here is the test case code:

struct S {
    def var1 : U32;
    def var2 : S32;
}

struct C {
    def v : S;
}

struct A1 {
    def a2 : A2;
}

struct A2 {
    def a1 : A1@;
}

You will notice that C has a member of type S, which was defined before it. This, of course, works without a problem.

On the other hand, A1 includes a full copy of A2, which is only defined down-source from it. What happens is that the compiler defers definition of A1 until A2 is fully defined, so if we look at the code generation (the output of practinop):

  Define struct A2 {
    a1 : Struct A1@
  }
  Define struct A1 {
    a2 : Struct A2
  }

We see that A2 is defined before A1. LLVM actually knows how to handle the other way around, but I did not want the front-end to rely on this.

We also see that we have a sort-of circular dependency here, in that A2 actually also depends on A1. Since it only needs a pointer to it, that’s fine, and compiles. If we remove the @, the code fails to compile:

14:1: error: Circular dependency in definition at 14:1

real    0m0.085s
user    0m0.039s
sys     0m0.034s

Importantly, it fails to compile quickly.

I know this took a long time to complete, but I’m really happy with how it turned out.

I still need to handle a few things:

  • Name mangling for structs. I’m incorporating a suggestion I got, and the name mangling will actually reflect the whole content of the struct. I’ll give more info on that later on.
  • I also realize it’s time to do something about the error messages. The compiler has a lot of information about what went wrong, and I need to start externalizing that information to the user.

I’m happy with the results, but do tell me what you think.
Shachar