src/builtin.lisp, src/codegen-proto.lisp: Improve slot initialization.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 21 Oct 2015 23:46:28 +0000 (00:46 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 7 Nov 2015 14:23:54 +0000 (14:23 +0000)
commit1d8206e9888ca942c039986ffb9c185c80b1e74c
tree10074ee4b8be76ae92682adb8118747c79ed90f1
parentca0c10c49568d1d340296ae9e613f32a6a31bfc9
src/builtin.lisp, src/codegen-proto.lisp: Improve slot initialization.

The fundamental difficulty is that actually we're not initializing an
instance's slots: we're assigning values to them.

Previously, we tried to distinguish scalar from aggregate initializers.
The former got converted into

sod__obj->nick.slot = EXPR;

while the latter ended up as

sod__obj->nick.slot = (TYPE) { COMPOUND-INIT };

There are two problems with this.  The first is easy: aggregate
literals, such are used in the latter case, are shiny and new in C99.
The second is more subtle: we distinguish aggregate from scalar
initializers from whether they're wrapped up in braces -- but in fact
they needn't be if they've been concealed in a macro, e.g.,
`PTHREAD_MUTEX_INITIALIZER', or `DSTR_INIT'.  Under the previous rules,
you'd have to write something like

pthread_mutex_t slot = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;

as your slot definition in order to get the right effect, and that's
just annoying.  (It would be a disservice to users to insert the cast
unconditionally, of course, since that would eliminate useful type
checking.)

Change all of this.  Instead, actually initialize an object of the right
type (we know that type that is, of course), and then assign it to the
slot.  A decent compiler will turn this into exactly the same object
code (yes, I tested this), and it means that our heuristic is no longer
needed for anything important.  I've preserved the distinction anyway,
because we can format compound initializers slightly more prettily, but
it no longer makes any semantic difference, and the dependency on C99
has been eliminated.
src/builtin.lisp
src/codegen-proto.lisp