Const variable declarations
Submitted by Gabriël Konat on 31 May 2013 at 12:09
Issue Log
C with const variables seems to have multiple problems. Do we want to support this const syntax and simply convert it to c and let the compiler deal with this? Or do we want provide a different means of const variables?
The link you sent is for C++, not C, so it does not apply. Const variables are used to indicate that a variable may not be assigned. This is done to prevent accidental assignments to variables that should not change, and to assist the compiler in optimisation. I think it’s an important feature that should be supported. Syntax wise it’s pretty simple, just add const in front of a variable. It has some implications on the type system and requires an additional constraint check to prevent assignment to const variables.
So const is just a simple modifier like volatile.
Than the plan is simple
- add to syntax
- add to transformation
- add type check constraint
Yes, but it does change the type of a variable from just
int
toconst int
, the type system has to be changed to support this. It also affects type equality, e.g. is aconst volatile int
type equal toint
?
Shouldn’t these modifiers not be a property of a type instead of a type itself?
No they are actually a new type. A property of a type for example is the size of that type. For an
int8
this is 1 byte and is true for the entire program. However, if you declare aconst int i
it does not mean that allint
types in the entire program are nowconst
.int
andconst int
are two different types.You can represent this in the type system as a list of modifiers:
int8: Type([], Int8())
,const int8: Type([Const()], Int8())
, etc.
syntax updated
Implemented and mapping to base-c implemented. Type-checking and mapping from Statemachine pending (other issues).
Log in to post comments