Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Note: Illegal Code in CTE

alexrp edited this page Mar 22, 2013 · 4 revisions

In the interest of portability and language implementability (this is a word now), CTE should disallow some non-portable language constructs. There are also certain language constructs that simply can't be done in CTE due to constraints imposed by Flect's compilation model.

  • External functions (those with non-flect linkage) cannot be called.
  • Functions for which the source code is neither present nor encoded in a metadata file cannot be called.
  • asm expressions cannot be executed.
  • Mutable global variables may not be referenced in any way.
  • Casts that throw away immutability in any way are illegal.
  • Taking the address of local variables (and generally anything on the stack) is illegal.
  • All pointer-to-pointer casts are illegal.
  • Pointer arithmetic and pointer indexing are mostly illegal. They are always illegal on null pointers.

Pointer arithmetic is allowed in some special cases:

  • Adding/subtracting zero to/from a pointer is always legal.
  • Adding and subtracting from a pointer are legal if the pointer points into an array or vector, and the resulting pointer still points into that same array or vector (or one element past).

Pointer dereferencing and indexing are allowed in some special cases:

  • Dereferencing a pointer is legal so long as it points inside an array or vector, or directly to something else (e.g. a field or a local). Note that this means that one-past-last-element pointers cannot be dereferenced. Dereferencing a pointer that points to a local which is no longer live is illegal.
  • Indexing a pointer at element zero is legal provided it satisfies the above condition (this operation is semantically equivalent to a plain dereference).
  • Indexing a pointer at a non-zero element is legal as long as the pointer points into an array or vector, and the element index is within the bounds of that array or vector.

There are a few restrictions on what types can be returned from CTE. Specifically, closure types cannot be returned. All other types are legal, as long as they do not contain closures. Note also that it is illegal to return a pointer to a local that is no longer live.