Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/typechecker/check-var-uses.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/typechecker/check-var-uses.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4021, Wed Jun 22 13:51:17 2016 UTC revision 4022, Wed Jun 22 14:29:50 2016 UTC
# Line 27  Line 27 
27    
28      fun error ((errStrm, _), msg) = Error.error (errStrm, TypeError.format msg)      fun error ((errStrm, _), msg) = Error.error (errStrm, TypeError.format msg)
29    
30      fun unusedWarning ((errStrm, _), x) =      fun warning ((errStrm, _), msg) = Error.warning (errStrm, TypeError.format msg)
31            Error.warning (errStrm, TypeError.format [  
32        fun uninitError (cxt, x) = error (cxt, [
33                S(Var.kindToString x), S " ", V x, S " declared at line ",
34                LN(Var.locationOf x), S " is not initialized"
35              ])
36    
37        fun unusedWarning (cxt, x) = warning (cxt, [
38                S(Var.kindToString x), S " ", V x, S " declared at line ",                S(Var.kindToString x), S " ", V x, S " declared at line ",
39                LN(Var.locationOf x), S " is unused"                LN(Var.locationOf x), S " is unused"
40              ])              ])
# Line 170  Line 176 
176      fun chkOptBlock (_, NONE, undef, unused) = (undef, unused)      fun chkOptBlock (_, NONE, undef, unused) = (undef, unused)
177        | chkOptBlock (cxt, SOME stm, undef, unused) = chkStmt (cxt,stm, undef, unused)        | chkOptBlock (cxt, SOME stm, undef, unused) = chkStmt (cxt,stm, undef, unused)
178    
179      fun chkStrand (cxt, AST.Strand{params, state, initM, updateM, stabilizeM, ...}, undef, unused) = let      fun chkStrand (cxt, strand, undef, unused) = let
180              val AST.Strand{params, state, stateInit, initM, updateM, stabilizeM, ...} = strand
181          (* check a state-variable declaration *)          (* check a state-variable declaration *)
182            fun chkVarDecl ((x, optE), (undef, unused, bound, outputs)) = let            fun chkVarDecl ((x, optE), (undef, unused, bound, outputs)) = let
183                  val (undef, unused) = (case optE                  val (undef, unused) = (case optE
# Line 189  Line 196 
196          (* process the state variables *)          (* process the state variables *)
197            val (undef, unused, bound, outputs) =            val (undef, unused, bound, outputs) =
198                  List.foldl chkVarDecl (undef, unused, bound, []) state                  List.foldl chkVarDecl (undef, unused, bound, []) state
199            (* check the state initialization block (if present) *)
200              val (undef, unused) = chkOptBlock (cxt, stateInit, undef, unused)
201            (* at this point we capture the set of uninitialized state variables *)
202              val uninit = let
203                    fun chk ((_, SOME _), s) = s
204                      | chk ((x, NONE), s) = if VSet.member(undef, x) then VSet.add(s, x) else s
205                    in
206                      List.foldl chk VSet.empty state
207                    end
208          (* check the initially method first (if it exists) *)          (* check the initially method first (if it exists) *)
209            val (undef, unused) = chkOptBlock (cxt, initM, undef, unused)            val (undef, unused) = chkOptBlock (cxt, initM, undef, unused)
210          (* then the update method *)          (* then the update method *)
# Line 200  Line 216 
216            val (undef'', unused) = chkOptBlock (cxt, stabilizeM, undef, unused)            val (undef'', unused) = chkOptBlock (cxt, stabilizeM, undef, unused)
217          (* merge the undef sets from the update and stabilize methods *)          (* merge the undef sets from the update and stabilize methods *)
218            val undef = VSet.intersection (undef', undef'')            val undef = VSet.intersection (undef', undef'')
219            (* state variables that are defined, but that are not initialized during strand creation *)
220              val uninit = VSet.difference(uninit, undef)
221            in            in
222          (* check for undefined output variables *)          (* check for undefined output variables *)
223              List.app              List.app
224                (fn x => if VSet.member(undef, x)                (fn x => if VSet.member(undef, x) then uninitError (cxt, x) else ())
225                    then error (cxt, [S "strand output variable ", V x, S " is not initialized"])                  outputs;
226    (* FIXME: we could change this to a warning, but we would need to correctly handle uninitialized
227     * variables during translation to High IR.
228     *)
229              (* report uninitialized state variables that are later defined *)
230                List.app
231                  (fn x => if VSet.member(uninit, x)
232                      then error (cxt, [
233                          S(Var.kindToString x), S " ", V x, S " declared at line ",
234                          LN(Var.locationOf x), S " is not initialized"
235                        ])
236                    else ()                    else ()
237                  ) outputs;                  ) outputs;
238            (* report unused state variables *)            (* report unused state variables *)
# Line 215  Line 243 
243        | chkCreate (cxt, AST.C_Collection stm, undef, unused) = #2 (chkStmt (cxt, stm, undef, unused))        | chkCreate (cxt, AST.C_Collection stm, undef, unused) = #2 (chkStmt (cxt, stm, undef, unused))
244    
245      fun check (cxt, prog) = let      fun check (cxt, prog) = let
246            val AST.Program{const_dcls, input_dcls, globals, init, strand, create, update, ...} = prog            val AST.Program{
247                      const_dcls, input_dcls, globals, globInit, strand, create, init, update, ...
248                    } = prog
249            fun chkVarDecl ((x, NONE), (undef, unused, bound)) =            fun chkVarDecl ((x, NONE), (undef, unused, bound)) =
250                  (VSet.add(undef, x), VSet.add(unused, x), VSet.add(bound, x))                  (VSet.add(undef, x), VSet.add(unused, x), VSet.add(bound, x))
251              | chkVarDecl ((x, SOME e), (undef, unused, bound)) =              | chkVarDecl ((x, SOME e), (undef, unused, bound)) =
# Line 240  Line 270 
270            val (undef, unused, bound) =            val (undef, unused, bound) =
271                  List.foldl chkGlobalDecl (undef, unused, bound) globals                  List.foldl chkGlobalDecl (undef, unused, bound) globals
272          (* the optional global initially block *)          (* the optional global initially block *)
273            val (undef, unused) = chkOptBlock (cxt, init, undef, unused)            val (undef, unused) = chkOptBlock (cxt, globInit, undef, unused)
274            (* capture the set of uninizialized globals (not including inputs) *)
275              val uninit = let
276                    fun chk (AST.D_Var(x, NONE), s) =
277                          if VSet.member(undef, x) then VSet.add(s, x) else s
278                      | chk (_, s) = s
279                    in
280                      List.foldl chk VSet.empty globals
281                    end
282          (* the strand (no global initialization allowed) *)          (* the strand (no global initialization allowed) *)
283            val unused = chkStrand (cxt, strand, undef, unused)            val unused = chkStrand (cxt, strand, undef, unused)
284          (* check the initial-strands creation code *)          (* check the initial-strands creation code *)
285            val unused = chkCreate (cxt, create, undef, unused)            val unused = chkCreate (cxt, create, undef, unused)
286            (* the optional global initially block *)
287              val (undef, unused) = chkOptBlock (cxt, init, undef, unused)
288          (* the optional global update block *)          (* the optional global update block *)
289            val (undef, unused) = chkOptBlock (cxt, update, undef, unused)            val (undef, unused) = chkOptBlock (cxt, update, undef, unused)
290            in            in
291              (* report uninitialized state variables *)
292                VSet.app (fn x => uninitError (cxt, x)) uninit;
293            (* report unused variables *)            (* report unused variables *)
294              VSet.app              VSet.app
295                (fn x => if VSet.member(unused, x) then unusedWarning(cxt, x) else ())                (fn x => if VSet.member(unused, x) then unusedWarning(cxt, x) else ())

Legend:
Removed from v.4021  
changed lines
  Added in v.4022

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0