package mopsa

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file operators.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(****************************************************************************)
(*                                                                          *)
(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)
(*                                                                          *)
(* Copyright (C) 2017-2019 The MOPSA Project.                               *)
(*                                                                          *)
(* 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.                                      *)
(*                                                                          *)
(* 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, see <http://www.gnu.org/licenses/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Mapping between operators and magic functions. *)

open Mopsa
open Universal.Ast
open Ast


(*==========================================================================*)
(**                      {2 Binary operators}                               *)
(*==========================================================================*)


(** Binary operator of a magic function *)
let fun_to_binop = function
  | "__add__" -> O_plus
  | "__sub__" -> O_minus
  | "__mul__" -> O_mult
  | "__matmul__" -> O_py_mat_mult
  | "__truediv__" -> O_div
  | "__floordiv__" -> O_py_floor_div
  | "__mod__" -> O_mod
  | "__pow__" -> O_pow
  | "__lshift__" -> O_bit_lshift
  | "__rshift__" -> O_bit_rshift
  | "__and__" -> O_bit_and
  | "__xor__" -> O_bit_xor
  | "__or__" -> O_bit_or
  | "__eq__" -> O_eq
  | "__ne__" -> O_ne
  | "__lt__" -> O_lt
  | "__le__" -> O_le
  | "__gt__" -> O_gt
  | "__ge__" -> O_ge
  | "__radd__" -> O_plus
  | "__rsub__" -> O_minus
  | "__rmul__" -> O_mult
  | "__rmatmul__" -> O_py_mat_mult
  | "__rtruediv__" -> O_div
  | "__rfloordiv__" -> O_py_floor_div
  | "__rmod__" -> O_mod
  | "__rpow__" -> O_pow
  | "__rlshift__" -> O_bit_lshift
  | "__rrshift__" -> O_bit_rshift
  | "__rand__" -> O_bit_and
  | "__rxor__" -> O_bit_xor
  | "__ror__" -> O_bit_or
  | "__iadd__" -> O_plus
  | "__isub__" -> O_minus
  | "__imul__" -> O_mult
  | "__imatmul__" -> O_py_mat_mult
  | "__itruediv__" -> O_div
  | "__ifloordiv__" -> O_py_floor_div
  | "__imod__" -> O_mod
  | "__ipow__" -> O_pow
  | "__ilshift__" -> O_bit_lshift
  | "__irshift__" -> O_bit_rshift
  | "__iand__" -> O_bit_and
  | "__ixor__" -> O_bit_xor
  | "__ior__" -> O_bit_or
  | s -> panic "Unknown operator %s" s

let methfun_to_binop str =
  let splitted = String.split_on_char '.' str in
  let l = ListExt.nth splitted (ListExt.length splitted - 1) in
  fun_to_binop l

(** Magic function of a binary operator *)
let binop_to_fun = function
  | O_plus -> "__add__"
  | O_minus -> "__sub__"
  | O_mult -> "__mul__"
  | O_py_mat_mult -> "__matmul__"
  | O_div -> "__truediv__"
  | O_py_floor_div -> "__floordiv__"
  | O_mod -> "__mod__"
  | O_pow -> "__pow__"
  | O_bit_lshift -> "__lshift__"
  | O_bit_rshift -> "__rshift__"
  | O_bit_and -> "__and__"
  | O_bit_xor -> "__xor__"
  | O_bit_or -> "__or__"
  | O_eq -> "__eq__"
  | O_ne -> "__ne__"
  | O_lt -> "__lt__"
  | O_le -> "__le__"
  | O_gt -> "__gt__"
  | O_ge -> "__ge__"
  | _ -> assert false

(** Right magic function of a binary operator *)
let binop_to_rev_fun = function
  | O_plus -> "__radd__"
  | O_minus -> "__rsub__"
  | O_mult -> "__rmul__"
  | O_py_mat_mult -> "__rmatmul__"
  | O_div -> "__rtruediv__"
  | O_py_floor_div -> "__rfloordiv__"
  | O_mod -> "__rmod__"
  | O_pow -> "__rpow__"
  | O_bit_lshift -> "__rlshift__"
  | O_bit_rshift -> "__rrshift__"
  | O_bit_and -> "__rand__"
  | O_bit_xor -> "__rxor__"
  | O_bit_or -> "__ror__"
  | _ -> assert false


(** Increment magic function of a binary operator *)
let binop_to_incr_fun = function
  | O_plus -> "__iadd__"
  | O_minus -> "__isub__"
  | O_mult -> "__imul__"
  | O_py_mat_mult -> "__imatmul__"
  | O_div -> "__itruediv__"
  | O_py_floor_div -> "__ifloordiv__"
  | O_mod -> "__imod__"
  | O_pow -> "__ipow__"
  | O_bit_lshift -> "__ilshift__"
  | O_bit_rshift -> "__irshift__"
  | O_bit_and -> "__iand__"
  | O_bit_xor -> "__ixor__"
  | O_bit_or -> "__ior__"
  | _ -> assert false


(** Check that a magic function corresponds to a binary operator *)
let is_binop_function = function
  | "__add__"
  | "__sub__"
  | "__mul__"
  | "__matmul__"
  | "__truediv__"
  | "__floordiv__"
  | "__mod__"
  | "__pow__"
  | "__lshift__"
  | "__rshift__"
  | "__and__"
  | "__xor__"
  | "__or__"
  | "__eq__"
  | "__ne__"
  | "__lt__"
  | "__le__"
  | "__gt__"
  | "__ge__"
  | "__iadd__"
  | "__isub__"
  | "__imul__"
  | "__imatmul__"
  | "__itruediv__"
  | "__ifloordiv__"
  | "__imod__"
  | "__ipow__"
  | "__ilshift__"
  | "__irshift__"
  | "__iand__"
  | "__ixor__"
  | "__ior__"
  | "__radd__"
  | "__rsub__"
  | "__rmul__"
  | "__rmatmul__"
  | "__rtruediv__"
  | "__rfloordiv__"
  | "__rmod__"
  | "__rpow__"
  | "__rlshift__"
  | "__rrshift__"
  | "__rand__"
  | "__rxor__"
  | "__ror__" -> true
  | _ -> false


(*==========================================================================*)
(**                       {2 Unary operators}                               *)
(*==========================================================================*)

(** Unary operator of a magic function *)
let fun_to_unop = function
  | "__not__" -> O_log_not
  | "__neg__" -> O_minus
  | "__pos__" -> O_plus
  | "__invert__" -> O_bit_invert
  | _ -> assert false


let methfun_to_unop str =
  let splitted = String.split_on_char '.' str in
  let l = ListExt.nth splitted (ListExt.length splitted - 1) in
  fun_to_unop l


(** Check that a magic function corresponds to a binary operator *)
let is_unop_function = function
  | "__not__"
  | "__neg__"
  | "__pos__"
  | "__invert__" -> true
  | _ -> false


(** Magic function of an unary operator *)
let unop_to_fun = function
  | O_log_not -> "__not__"
  | O_plus -> "__pos__"
  | O_minus -> "__neg__"
  | O_bit_invert -> "__invert__"
  | _ -> assert false
OCaml

Innovation. Community. Security.