Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / type_cocci.ml
CommitLineData
9f8e26f4 1(*
ae4735db 2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
9f8e26f4
C
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
5636bb2c
C
23(*
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
27 *
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
31 *
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
39 *
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
42 *)
43
44
34e49164
C
45(* for metavariables in general, but here because needed for metatypes *)
46type inherited = bool (* true if inherited *)
47type keep_binding = Unitary (* need no info *)
48 | Nonunitary (* need an env entry *) | Saved (* need a witness *)
49
faf9a90c 50type typeC =
34e49164 51 ConstVol of const_vol * typeC
faf9a90c
C
52 | BaseType of baseType
53 | SignedT of sign * typeC option
34e49164
C
54 | Pointer of typeC
55 | FunctionPointer of typeC (* only return type *)
56 | Array of typeC (* drop size info *)
faf9a90c 57 | EnumName of bool (* true if a metaId *) * string
34e49164
C
58 | StructUnionName of structUnion * bool (* true if a metaId *) * string
59 | TypeName of string
60 | MetaType of (string * string) * keep_binding * inherited
61 | Unknown (* for metavariables of type expression *^* *)
62
63and tagged_string = string
faf9a90c 64
34e49164 65and baseType = VoidType | CharType | ShortType | IntType | DoubleType
faf9a90c 66| FloatType | LongType | LongLongType | BoolType
34e49164
C
67
68and structUnion = Struct | Union
69
70and sign = Signed | Unsigned
71
72and const_vol = Const | Volatile
73
74(* --------------------------------------------------------------------- *)
75(* Printer *)
faf9a90c
C
76open Format
77
78let rec type2c = function
79 ConstVol(cv,ty) -> (const_vol cv) ^ (type2c ty)
80 | BaseType(ty) -> baseType ty
81 | SignedT(sgn,None) -> sign sgn
82 | SignedT(sgn,Some ty) -> (sign sgn) ^ (type2c ty)
83 | Pointer(ty) -> (type2c ty) ^ "*"
84 | FunctionPointer(ty) -> (type2c ty) ^ "(*)(...)"
85 | Array(ty) -> (type2c ty) ^ "[] "
86 | EnumName(mv,name) -> "enum " ^ name ^ " "
87 | StructUnionName(kind,mv,name) -> (structUnion kind) ^ name ^ " "
88 | TypeName(name) -> name ^ " "
89 | MetaType((rule,name),keep,inherited) -> name ^ " "
34e49164
C
90 (*
91 let print_unitary = function
92 Unitary -> print_string "unitary"
93 | Nonunitary -> print_string "nonunitary"
94 | Saved -> print_string "saved" in
95 print_string "/* ";
96 print_string "keep:"; print_unitary keep;
97 print_string " inherited:"; print_bool inherited;
98 print_string " */"
99 *)
faf9a90c 100 | Unknown -> "unknown "
34e49164
C
101
102and baseType = function
faf9a90c
C
103 VoidType -> "void "
104 | CharType -> "char "
105 | ShortType -> "short "
106 | IntType -> "int "
107 | DoubleType -> "double "
108 | FloatType -> "float "
109 | LongType -> "long "
110 | LongLongType -> "long long "
111 | BoolType -> "bool "
34e49164
C
112
113and structUnion = function
faf9a90c
C
114 Struct -> "struct "
115 | Union -> "union "
34e49164
C
116
117and sign = function
faf9a90c
C
118 Signed -> "signed "
119 | Unsigned -> "unsigned "
34e49164
C
120
121and const_vol = function
faf9a90c
C
122 Const -> "const "
123 | Volatile -> "volatile "
124
125let typeC t = print_string (type2c t)
34e49164
C
126
127(* t1 should be less informative than t1, eg t1 = Pointer(Unknown) and t2 =
128Pointer(int) *)
129(* only used in iso *)
130(* needs to do something for MetaType *)
131let compatible t1 = function
132 None -> t1 = Unknown
133 | Some t2 ->
134 let rec loop = function
135 (Unknown,_) -> true
136 | (ConstVol(cv1,ty1),ConstVol(cv2,ty2)) when cv1 = cv2 ->
137 loop(ty1,ty2)
138 | (Pointer(ty1),Pointer(ty2)) -> loop(ty1,ty2)
139 | (FunctionPointer(ty1),_) -> false (* not enough info *)
140 | (_,FunctionPointer(ty2)) -> false (* not enough info *)
141 | (Array(ty1),Array(ty2)) -> loop(ty1,ty2)
142 | (_,_) -> t1=t2 in
143 loop (t1,t2)