|
| 1 | +(* BuckleScript compiler |
| 2 | + * Copyright (C) 2015-2016 Bloomberg Finance L.P. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, with linking exception; |
| 7 | + * either version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + *) |
| 18 | + |
| 19 | +(* Author: Hongbo Zhang *) |
| 20 | + |
| 21 | +let rec has_exit_code exits (lam : Lambda.lambda) : bool = |
| 22 | + match lam with |
| 23 | + | Lambda.Lvar _ |
| 24 | + | Lambda.Lconst _ |
| 25 | + | Lambda.Lfunction _ (* static exit can not across function boundary *) |
| 26 | + -> false |
| 27 | + | Lambda.Lapply (l,args,_apply_info) |
| 28 | + -> has_exit_code exits l || List.exists (fun x -> has_exit_code exits x ) args |
| 29 | + |
| 30 | + | Lambda.Llet (_kind,_id,v,body) |
| 31 | + -> has_exit_code exits v || has_exit_code exits body |
| 32 | + | Lambda.Lletrec (binding,body) -> |
| 33 | + List.exists (fun (_, l) -> has_exit_code exits l ) binding || |
| 34 | + has_exit_code exits body |
| 35 | + | Lambda.Lprim (_,ls) |
| 36 | + -> List.exists (fun x -> has_exit_code exits x) ls |
| 37 | + | Lambda.Lswitch (l,lam_switch) |
| 38 | + -> has_exit_code exits l || has_exit_code_lam_switch exits lam_switch |
| 39 | + |
| 40 | + | Lambda.Lstringswitch (l,ls,opt) -> |
| 41 | + has_exit_code exits l || |
| 42 | + List.exists (fun (_,l) -> has_exit_code exits l) ls || |
| 43 | + (match opt with |
| 44 | + | None -> false |
| 45 | + | Some x -> has_exit_code exits l ) |
| 46 | + | Lambda.Lstaticraise (v,ls) -> |
| 47 | + exits v || |
| 48 | + List.exists (has_exit_code exits) ls |
| 49 | + | Lambda.Lstaticcatch (l,_,handler) |
| 50 | + -> |
| 51 | + has_exit_code exits l || has_exit_code exits handler |
| 52 | + | Lambda.Ltrywith (l,_, handler) |
| 53 | + -> |
| 54 | + has_exit_code exits l || has_exit_code exits handler |
| 55 | + | Lambda.Lifthenelse (a,b,c) |
| 56 | + -> |
| 57 | + has_exit_code exits a || has_exit_code exits b || has_exit_code exits c |
| 58 | + | Lambda.Lsequence (a,b) |
| 59 | + -> |
| 60 | + has_exit_code exits a || has_exit_code exits b |
| 61 | + | Lambda.Lwhile (a,b) |
| 62 | + -> |
| 63 | + has_exit_code exits a || has_exit_code exits b |
| 64 | + | Lambda.Lfor (_,a,b,_dir,body) -> |
| 65 | + has_exit_code exits a |
| 66 | + || has_exit_code exits b |
| 67 | + || has_exit_code exits body |
| 68 | + |
| 69 | + | Lambda.Lassign (_,a) |
| 70 | + -> |
| 71 | + has_exit_code exits a |
| 72 | + | Lambda.Lsend (_,obj,l,ls,_loc) |
| 73 | + -> |
| 74 | + has_exit_code exits obj || |
| 75 | + has_exit_code exits l || |
| 76 | + List.exists (has_exit_code exits) ls |
| 77 | + | Lambda.Levent (b,_) |
| 78 | + -> has_exit_code exits b |
| 79 | + | Lambda.Lifused (_,b) |
| 80 | + -> has_exit_code exits b |
| 81 | + |
| 82 | +and has_exit_code_lam_switch exits (lam_switch : Lambda.lambda_switch) = |
| 83 | + match lam_switch with |
| 84 | + | { sw_numconsts = _; sw_consts; sw_numblocks = _ ; sw_blocks; sw_failaction } -> |
| 85 | + List.exists (fun (_,l) -> has_exit_code exits l) sw_consts || |
| 86 | + List.exists (fun (_,l) -> has_exit_code exits l) sw_blocks || |
| 87 | + (match sw_failaction with |
| 88 | + | None -> false |
| 89 | + | Some x -> has_exit_code exits x) |
0 commit comments