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