|
| 1 | +(* Copyright (C) 2018 Authors of BuckleScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | +open Ast_helper |
| 26 | + |
| 27 | + |
| 28 | +let newTdcls |
| 29 | + (tdcls : Parsetree.type_declaration list) |
| 30 | + (newAttrs : Parsetree.attributes) |
| 31 | + : Parsetree.type_declaration list |
| 32 | + = |
| 33 | + match tdcls with |
| 34 | + | [ x ] -> |
| 35 | + [{ x with Parsetree.ptype_attributes = newAttrs}] |
| 36 | + | _ -> |
| 37 | + Ext_list.map_last |
| 38 | + (fun last x -> |
| 39 | + if last then |
| 40 | + { x with |
| 41 | + Parsetree.ptype_attributes = newAttrs} |
| 42 | + else x ) |
| 43 | + tdcls |
| 44 | + |
| 45 | + |
| 46 | +let handleTdclsInSigi |
| 47 | + (self : Bs_ast_mapper.mapper) |
| 48 | + (sigi : Parsetree.signature_item) |
| 49 | + (tdcls : Parsetree.type_declaration list) |
| 50 | + : Ast_signature.item = |
| 51 | + begin match Ast_attributes.process_derive_type |
| 52 | + (Ext_list.last tdcls).ptype_attributes with |
| 53 | + | {bs_deriving = Some actions; explict_nonrec}, newAttrs |
| 54 | + -> |
| 55 | + let loc = sigi.psig_loc in |
| 56 | + let newTdcls = newTdcls tdcls newAttrs in |
| 57 | + let newSigi = |
| 58 | + self.signature_item self {sigi with psig_desc = Psig_type newTdcls} in |
| 59 | + if Ast_payload.isAbstract actions then |
| 60 | + let codes = Ast_derive_abstract.handleTdclsInSig newTdcls in |
| 61 | + Ast_signature.fuseAll ~loc |
| 62 | + ( |
| 63 | + Sig.include_ ~loc |
| 64 | + (Incl.mk ~loc |
| 65 | + (Mty.typeof_ ~loc |
| 66 | + (Mod.constraint_ ~loc |
| 67 | + (Mod.structure ~loc [ |
| 68 | + { pstr_loc = loc; |
| 69 | + pstr_desc = |
| 70 | + Pstr_type |
| 71 | + (match newSigi.psig_desc with |
| 72 | + | Psig_type x -> x |
| 73 | + | _ -> assert false) |
| 74 | + }] ) |
| 75 | + (Mty.signature ~loc [])) ) ) |
| 76 | + :: |
| 77 | + self.signature self |
| 78 | + codes |
| 79 | + ) |
| 80 | + else |
| 81 | + Ast_signature.fuseAll ~loc |
| 82 | + (newSigi:: |
| 83 | + self.signature |
| 84 | + self |
| 85 | + ( |
| 86 | + Ast_derive.gen_signature tdcls actions explict_nonrec)) |
| 87 | + | {bs_deriving = None }, _ -> |
| 88 | + Bs_ast_mapper.default_mapper.signature_item self sigi |
| 89 | + |
| 90 | + end |
| 91 | + |
| 92 | + |
| 93 | +let handleTdclsInStru |
| 94 | + (self : Bs_ast_mapper.mapper) |
| 95 | + (str : Parsetree.structure_item) |
| 96 | + (tdcls : Parsetree.type_declaration list) |
| 97 | + : Ast_structure.item = |
| 98 | + begin match |
| 99 | + Ast_attributes.process_derive_type |
| 100 | + ((Ext_list.last tdcls).ptype_attributes) with |
| 101 | + | {bs_deriving = Some actions; |
| 102 | + explict_nonrec |
| 103 | + }, newAttrs -> |
| 104 | + let loc = str.pstr_loc in |
| 105 | + let tdcls2 = newTdcls tdcls newAttrs in |
| 106 | + let newStr = |
| 107 | + self.structure_item self |
| 108 | + {str with pstr_desc = Pstr_type tdcls2} in |
| 109 | + if Ast_payload.isAbstract actions then |
| 110 | + let codes = Ast_derive_abstract.handleTdclsInStr tdcls2 in |
| 111 | + (* use [tdcls2] avoid nonterminating *) |
| 112 | + Ast_structure.fuseAll ~loc |
| 113 | + ( |
| 114 | + Ast_structure.constraint_ ~loc |
| 115 | + [newStr] []:: |
| 116 | + self.structure self |
| 117 | + codes) |
| 118 | + else |
| 119 | + Ast_structure.fuseAll ~loc |
| 120 | + (newStr :: |
| 121 | + self.structure self |
| 122 | + ( |
| 123 | + List.map |
| 124 | + (fun action -> |
| 125 | + Ast_derive.gen_structure_signature |
| 126 | + loc |
| 127 | + tdcls action explict_nonrec |
| 128 | + ) actions |
| 129 | + )) |
| 130 | + | {bs_deriving = None }, _ -> |
| 131 | + Bs_ast_mapper.default_mapper.structure_item self str |
| 132 | + end |
| 133 | + |
| 134 | + |
0 commit comments