package mopsa

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

Source file ast.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
(**

  Copyright (c) 2017-2019 Aymeric Fromherz and The MOPSA Project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
 *)

(**
   AST - A more abstract representation than the original Python CST.

   Contains some additional static information, such as unique variables IDs and
   the list of local variables of functions.
*)


open Mopsa_utils

 (** A variable with a unique identifier *)
type var = {
  name: string; (** original name in the source code *)
  uid: int; (** a unique identifier at the scope level *)
}

type program = {
  prog_body : stmt;
  prog_globals : var list;
}

(** Statements *)
and stmt = {
  skind : stmt_kind;
  srange : Location.range;
}

and binop =
  | O_arithmetic of Cst.binop
  | O_comparison of Cst.cmpop
  | O_bool of Cst.boolop

and stmt_kind =
  | S_assign
    of expr (** target *) *
       expr (** value *)

  | S_type_annot of expr (** target *) * expr (** value *)

  | S_expression
    of expr (** expression statements *)

  | S_while
    of expr (** test *) *
       stmt (** body *) *
       stmt option (** else *)

  | S_break

  | S_continue

  | S_block
    of stmt list

  | S_aug_assign (** such as x += e *)
    of expr (** taregt *) *
       binop (** operator *) *
       expr (** value *)

  | S_if
    of expr (** test *) *
       stmt (** then branch *) *
       stmt option (** optional else branch *)

  | S_function (** function definition *)
    of func

  | S_class (** class definition *)
    of cls

  | S_for (** for loops *)
    of expr (** target *) *
       expr (** iterator *) *
       stmt (** body *) *
       stmt option (** else *)

  | S_return (** return *)
    of expr (** return expression. Empty returns are equivalent to return None *)

  | S_raise of expr option (** exn *) * expr option (** cause *)

  (** try/except statement *)
  | S_try
    of stmt (** body *) *
       except list (** except handlers *)  *
       stmt option (** else body *) *
       stmt option (** final body *)

  | S_import of string (** module *) * var option (** asname *) * var (** root module var *)
  | S_import_from of string (** module *) * string (** name *) * var (** root module var *) * var (** module var *)
  (** Import statements *)

  | S_delete of expr

  | S_assert of expr * expr option

  | S_with of expr (** context *) * expr option (** as *) * stmt (** body *)

  | S_pass

(** Exception handler *)
and except =
  expr option (** exception type *) *
  var option (** as name *) *
  stmt (** body *)

(** Function declaration *)
and func = {
  func_var: var; (** function object variable *)
  func_parameters: var list; (** list of parameters variables *)
  func_defaults: expr option list; (** list of default parameters values *)
  func_vararg: var option; (* variable argument arg (usually *args), if any *)
  func_kwonly_args: var list; (* list of keyword-only arguments *)
  func_kwonly_defaults: expr option list; (* default values associated to keyword-only arguments *)
  func_kwarg: var option; (* keyword-based variable argument (usually **kwargs) if any *)
  func_locals: var list; (** list of local variables *)
  func_globals: var list; (** list of variables declared as global *)
  func_nonlocals: var list; (** list of variables declared as nonlocal *)
  func_body: stmt; (** function body *)
  func_is_generator: bool; (** is the function a generator? *)
  func_decors: expr list;
  func_types_in: expr option list; (* type of the arguments *)
  func_type_out: expr option; (* type of the return *)
  func_range: Location.range;
}

(** Class definition *)
and cls = {
  cls_var: var; (** class object variable *)
  cls_body: stmt; (** class body *)
  cls_static_attributes: var list;
  cls_bases: expr list; (** inheritance base classes *)
  cls_decors: expr list;
  cls_keywords: (string option * expr) list; (** keywords (None id for **kwargs) *)
  cls_range: Location.range;
}


and lambda = {
  lambda_body: expr;
  lambda_parameters: var list; (** list of parameters variables *)
  lambda_defaults: expr option list; (** list of default parameters values *)
}

(** Expressions *)
and expr = {
  ekind : expr_kind;
  erange : Location.range;
}

and expr_kind =
  | E_ellipsis
    | E_true

    | E_false

    | E_none

    | E_notimplemented

    | E_num of Cst.number

    | E_str of string

    | E_bytes of string

    (** attribute access *)
    | E_attr of
        expr (** object *) *
        string (** attribute name *)

    | E_id of var

    (** function call *)
    | E_call of
        expr (** function expression *)*
        expr list (** arguments *) *
        (string option * expr) list (** keywords (None id for **kwargs) *)

    | E_list of expr list (** list of elements *)

    (** Index-based subscript access *)
    | E_index_subscript of
        expr (** object *) * expr (** index *)

    (** Slice-based subscript access *)
    | E_slice_subscript of
        expr (** object *) *
        expr (** Lower bound. None if not specified *) *
        expr (** Uper bound. None if not specified *) *
        expr (** Step. None if not specified *)

    | E_tuple of expr list

    | E_set of expr list

    | E_dict of expr list (** keys *) * expr list (** values *)

    (** Generator comprehension *)
    | E_generator_comp of
        expr (** generator expression *) *
        comprehension list (** list of comprehensions *)

    (** List comprehension *)
    | E_list_comp of
        expr (** element expression *) *
        comprehension list (** list of comprehensions *)

    (** Set comprehension *)
    | E_set_comp of
        expr (** element expression *) *
        comprehension list (** list of comprehensions *)

    (** Dictionary comprehension *)
    | E_dict_comp of
        expr (** key expression *) *
        expr (** value expression *) *
        comprehension list (** list of comprehensions *)

    (** If expressions *)
    | E_if of
        expr (* test *) *
        expr (* body *) *
        expr (* else *)


    (** Yield expression *)
    | E_yield of expr
    | E_yield_from of expr
    (** Binary operator expressions *)
    | E_binop of
        expr (** left operand *) * binop * expr (** right operand *)

    | E_multi_compare of expr (* left *)
                         * binop list (* ops *)
                         * expr list (* comparators *)


    (** Unary operato expressions *)
    | E_unop of Cst.unop * expr

    | E_lambda of lambda

(** Comprehensions *)
and comprehension =
  expr (** target *) *
  expr (** iterator *) *
  expr list (** list of conditions *)


module VarSet = Set.Make(struct type t = var let compare = compare end)
module VarMap = MapExt.Make(struct type t = var let compare = compare end)
module VarSetMap = MapExt.Make(struct type t = VarSet.t let compare = VarSet.compare end)
OCaml

Innovation. Community. Security.