-
Notifications
You must be signed in to change notification settings - Fork 570
/
Type_generic.ml
68 lines (62 loc) · 2.36 KB
/
Type_generic.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
(* Yoann Padioleau
*
* Copyright (C) 2022 r2c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library 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 file
* license.txt for more details.
*)
module G = AST_generic
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Common representation for (builtin) types across languages.
*
* In certain languages, a boolean declaration is using the type name
* "boolean", in another language "Bool", in yet another "bool".
* This is tedious to handle all those specifities during matching, so
* the goal of this file is to factorize those specifities here.
*
* alt:
* - store this directly in AST_generic.ml and do the work in the
* xxx_to_generic.ml
*
* related:
* - Type.ml in deep-semgrep
*)
(*****************************************************************************)
(* Types *)
(*****************************************************************************)
(*
* coupling: this is mostly used for typed metavariables like '($X: int)' to
* match literals like 'f(1)', so most of the types below should have
* a corresponding construct in AST_generic.literal
*)
type builtin_type = TInt | TNumber | TString | TFloat [@@deriving show]
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
(* less: should sanity check things by looking at [lang] *)
let builtin_type_of_ident _langTODO str =
match str with
| "int" -> Some TInt
| "float" -> Some TFloat
| "str"
| "string"
| "String" ->
Some TString
| _ -> None
let builtin_type_of_type lang t =
match t.G.t with
(* for Python literal checking *)
| G.TyExpr { e = G.N (G.Id ((str, _t), _idinfo)); _ } ->
builtin_type_of_ident lang str
(* for Java/Go/... literals *)
| G.TyN (Id ((str, _t), _idinfo)) -> builtin_type_of_ident lang str
| _ -> None