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