Skip to content

Implement constants using & but not ~ or @ #2317

Closed
@graydon

Description

@graydon

I think we have a reasonable answer for structured constant expressions now, which is to construct them using &-pointers rather than boxes. Boxes are for dynamic allocation only.

I believe issues #1272, #1417, #571 and #570 all relate to this and should probably be merged into this one. I think the correct course of action is to define two subsets of the expression language:

  • Constant expressions (a subset of expressions)
  • Integer-constant expressions (a subset of constant expressions)

The compiler should have a pass that classifies each expression as either dynamic, constant, or integer-constant, based on its operators and operands.

Only constant expressions should be legal initializers for a const item. The compiler back-end can evaluate constant expressions to read-only memory using the LLVM constant instuctions.

The compiler front-end can evaluate integer-constant expressions, in order to deduce array bounds, structure sizes, enumeration discriminators and similar "size-like" values. This is what we're currently using eval_const_expr for (in bug #1417) and it should be rigorously defined as part of the language. In particular, we might in the future permit integer-constant expressions to show up inside portions of the type grammar (particularly relating to constant sizes given for arrays).

The compiler back-end should arrive at the exact same integer values as the front-end did, when translating an integer-constant expression.

We may also wish to look into a form of const fn as in C++0x constexpr functions. At this point I think it's effectively already reserved syntax for us so we can punt for now.

It may also be useful to add sizeof(), alignof(), and similar "built-in" constant functions implemented using compiler intrinsics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions