@@ -152,19 +152,28 @@ class Variable extends Value {
152152 msg : "Cannot give ownership of a borrowed value to a child function" ,
153153 ref
154154 } ;
155+ } else if ( this . type . constant ) {
156+ return {
157+ error : true ,
158+ msg : `Cannot consume a constant value\n Recommend cloning $${ this . name } ` ,
159+ ref
160+ } ;
155161 }
156162
157- this . makeUndefined ( ) ;
163+ this . makeUndefined ( ref ) ;
158164 this . lastUninit = ref . start ;
159165 }
160166
161167 out . type = this . type ;
162168 return out ;
163169 }
164170
165- makeUndefined ( ) {
166- this . store = null ;
171+ makeUndefined ( ref ) {
172+ this . elements . clear ( ) ;
173+ this . isDecomposed = false ;
167174 this . probability = null ;
175+ this . store = null ;
176+ this . lastUninit = ref . start ;
168177 }
169178
170179 /**
@@ -175,19 +184,26 @@ class Variable extends Value {
175184 * @returns {Error? }
176185 */
177186 markUpdated ( register , force = false , ref ) {
178- if ( ! force && this . type . lent ) {
179- return {
180- error : true ,
181- msg : "Cannot overwite a lent value" ,
182- ref
183- } ;
187+ if ( ! force ) {
188+ if ( this . type . lent ) {
189+ return {
190+ error : true ,
191+ msg : "Cannot overwite a lent value" ,
192+ ref
193+ } ;
194+ } else if ( this . type . constant ) {
195+ return {
196+ error : true ,
197+ msg : "Cannot overwite a constant value" ,
198+ ref
199+ } ;
200+ }
184201 }
185202
186- this . store = register ;
203+ this . isDecomposed = false ;
187204 this . probability = null ;
188- this . lastUninit = null ;
189205 this . hasUpdated = true ;
190- this . isDecomposed = false ;
206+ this . store = register ;
191207
192208 return true ;
193209 }
@@ -235,6 +251,7 @@ class Variable extends Value {
235251 let out ;
236252 if ( ! this . elements . has ( gep . index ) ) {
237253 let read = struct . accessGEPByIndex ( gep . index , this . store ) ;
254+ read . type . constant = read . type . constant || this . type . constant ;
238255 out = new Variable (
239256 read . type ,
240257 `${ this . name } .${ accessor } ` ,
@@ -712,7 +729,7 @@ class Variable extends Value {
712729 cleanup ( ref ) {
713730 let frag = new LLVM . Fragment ( ) ;
714731
715- if ( this . isClone ) { // Do nothing as this variable is a clone
732+ if ( this . isClone || this . type . constant ) { // Do nothing as this variable is a clone/constant
716733 return frag ;
717734 } else if ( this . type . lent ) { // Borrowed types need to be recomposed
718735 let res = this . resolve ( ref , false ) ;
@@ -750,13 +767,15 @@ class Variable extends Value {
750767 msg : "Cannot delete an already undefined value" ,
751768 ref : ref
752769 } ;
770+ } else if ( this . type . constant ) {
771+ return {
772+ error : true ,
773+ msg : "Cannot delete a constant value" ,
774+ ref : ref
775+ } ;
753776 }
754777
755- this . elements = new Map ( ) ;
756- this . isDecomposed = false ;
757- this . probability = null ;
758- this . store = null ;
759- this . lastUninit = ref . start ;
778+ this . makeUndefined ( ref ) ;
760779 return new LLVM . Fragment ( ) ;
761780 }
762781}
0 commit comments