Release coccinelle-0.2.4rc1
[bpt/coccinelle.git] / parsing_cocci / type_cocci.ml
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
25 (* for metavariables in general, but here because needed for metatypes *)
26 type inherited = bool (* true if inherited *)
27 type keep_binding = Unitary (* need no info *)
28 | Nonunitary (* need an env entry *) | Saved (* need a witness *)
29
30 type typeC =
31 ConstVol of const_vol * typeC
32 | BaseType of baseType
33 | SignedT of sign * typeC option
34 | Pointer of typeC
35 | FunctionPointer of typeC (* only return type *)
36 | Array of typeC (* drop size info *)
37 | EnumName of bool (* true if a metaId *) * string
38 | StructUnionName of structUnion * bool (* true if a metaId *) * string
39 | TypeName of string
40 | MetaType of (string * string) * keep_binding * inherited
41 | Unknown (* for metavariables of type expression *^* *)
42
43 and tagged_string = string
44
45 and baseType = VoidType | CharType | ShortType | IntType | DoubleType
46 | FloatType | LongType | LongLongType | BoolType
47
48 and structUnion = Struct | Union
49
50 and sign = Signed | Unsigned
51
52 and const_vol = Const | Volatile
53
54 (* --------------------------------------------------------------------- *)
55 (* Printer *)
56 open Format
57
58 let rec type2c = function
59 ConstVol(cv,ty) -> (const_vol cv) ^ (type2c ty)
60 | BaseType(ty) -> baseType ty
61 | SignedT(sgn,None) -> sign sgn
62 | SignedT(sgn,Some ty) -> (sign sgn) ^ (type2c ty)
63 | Pointer(ty) -> (type2c ty) ^ "*"
64 | FunctionPointer(ty) -> (type2c ty) ^ "(*)(...)"
65 | Array(ty) -> (type2c ty) ^ "[] "
66 | EnumName(mv,name) -> "enum " ^ name ^ " "
67 | StructUnionName(kind,mv,name) -> (structUnion kind) ^ name ^ " "
68 | TypeName(name) -> name ^ " "
69 | MetaType((rule,name),keep,inherited) -> name ^ " "
70 (*
71 let print_unitary = function
72 Unitary -> print_string "unitary"
73 | Nonunitary -> print_string "nonunitary"
74 | Saved -> print_string "saved" in
75 print_string "/* ";
76 print_string "keep:"; print_unitary keep;
77 print_string " inherited:"; print_bool inherited;
78 print_string " */"
79 *)
80 | Unknown -> "unknown "
81
82 and baseType = function
83 VoidType -> "void "
84 | CharType -> "char "
85 | ShortType -> "short "
86 | IntType -> "int "
87 | DoubleType -> "double "
88 | FloatType -> "float "
89 | LongType -> "long "
90 | LongLongType -> "long long "
91 | BoolType -> "bool "
92
93 and structUnion = function
94 Struct -> "struct "
95 | Union -> "union "
96
97 and sign = function
98 Signed -> "signed "
99 | Unsigned -> "unsigned "
100
101 and const_vol = function
102 Const -> "const "
103 | Volatile -> "volatile "
104
105 let typeC t = print_string (type2c t)
106
107 (* t1 should be less informative than t1, eg t1 = Pointer(Unknown) and t2 =
108 Pointer(int) *)
109 (* only used in iso *)
110 (* needs to do something for MetaType *)
111 let compatible t1 = function
112 None -> t1 = Unknown
113 | Some t2 ->
114 let rec loop = function
115 (Unknown,_) -> true
116 | (ConstVol(cv1,ty1),ConstVol(cv2,ty2)) when cv1 = cv2 ->
117 loop(ty1,ty2)
118 | (Pointer(ty1),Pointer(ty2)) -> loop(ty1,ty2)
119 | (FunctionPointer(ty1),_) -> false (* not enough info *)
120 | (_,FunctionPointer(ty2)) -> false (* not enough info *)
121 | (Array(ty1),Array(ty2)) -> loop(ty1,ty2)
122 | (_,_) -> t1=t2 in
123 loop (t1,t2)