coccinelle release 1.0.0-rc6
[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 meta_name = string * string (*Ast_cocci.meta_name*)
31
32 type typeC =
33 ConstVol of const_vol * typeC
34 | BaseType of baseType
35 | SignedT of sign * typeC option
36 | Pointer of typeC
37 | FunctionPointer of typeC (* only return type *)
38 | Array of typeC (* drop size info *)
39 | EnumName of name
40 | StructUnionName of structUnion * name
41 | TypeName of string
42 | MetaType of meta_name * keep_binding * inherited
43 | Unknown (* for metavariables of type expression *^* *)
44
45 and name =
46 NoName
47 | Name of string
48 | MV of meta_name * keep_binding * inherited
49
50 and tagged_string = string
51
52 and baseType = VoidType | CharType | ShortType | ShortIntType | IntType
53 | DoubleType | LongDoubleType | FloatType
54 | LongType | LongIntType | LongLongType | LongLongIntType
55 | SizeType | SSizeType | PtrDiffType | BoolType
56
57 and structUnion = Struct | Union
58
59 and sign = Signed | Unsigned
60
61 and const_vol = Const | Volatile
62
63 (* --------------------------------------------------------------------- *)
64 (* Printer *)
65 open Format
66
67 let rec type2c = function
68 ConstVol(cv,ty) -> (const_vol cv) ^ (type2c ty)
69 | BaseType(ty) -> baseType ty
70 | SignedT(sgn,None) -> sign sgn
71 | SignedT(sgn,Some ty) -> (sign sgn) ^ (type2c ty)
72 | Pointer(ty) -> (type2c ty) ^ "*"
73 | FunctionPointer(ty) -> (type2c ty) ^ "(*)(...)"
74 | Array(ty) -> (type2c ty) ^ "[] "
75 | EnumName(name) -> "enum " ^ (print_name name)
76 | StructUnionName(kind,name) -> (structUnion kind) ^ (print_name name)
77 | TypeName(name) -> name ^ " "
78 | MetaType((rule,name),keep,inherited) -> name ^ " "
79 (*
80 let print_unitary = function
81 Unitary -> print_string "unitary"
82 | Nonunitary -> print_string "nonunitary"
83 | Saved -> print_string "saved" in
84 print_string "/* ";
85 print_string "keep:"; print_unitary keep;
86 print_string " inherited:"; print_bool inherited;
87 print_string " */"
88 *)
89 | Unknown -> "unknown "
90
91 and print_name = function
92 NoName -> ""
93 | MV ((_,name),_,_) -> name ^ " "
94 | Name name -> name ^ " "
95
96 and baseType = function
97 VoidType -> "void "
98 | CharType -> "char "
99 | ShortType -> "short "
100 | ShortIntType -> "short int "
101 | IntType -> "int "
102 | DoubleType -> "double "
103 | LongDoubleType -> "long double "
104 | FloatType -> "float "
105 | LongType -> "long "
106 | LongIntType -> "long int "
107 | LongLongType -> "long long "
108 | LongLongIntType -> "long long int "
109 | BoolType -> "bool "
110 | SizeType -> "size_t "
111 | SSizeType -> "ssize_t "
112 | PtrDiffType -> "ptrdiff_t "
113
114
115 and structUnion = function
116 Struct -> "struct "
117 | Union -> "union "
118
119 and sign = function
120 Signed -> "signed "
121 | Unsigned -> "unsigned "
122
123 and const_vol = function
124 Const -> "const "
125 | Volatile -> "volatile "
126
127 let typeC t = print_string (type2c t)
128
129 (* t1 should be less informative than t1, eg t1 = Pointer(Unknown) and t2 =
130 Pointer(int) *)
131 (* only used in iso *)
132 (* needs to do something for MetaType *)
133 let compatible t1 = function
134 None -> t1 = Unknown
135 | Some t2 ->
136 let rec loop = function
137 (Unknown,_) -> true
138 | (ConstVol(cv1,ty1),ConstVol(cv2,ty2)) when cv1 = cv2 ->
139 loop(ty1,ty2)
140 | (Pointer(ty1),Pointer(ty2)) -> loop(ty1,ty2)
141 | (FunctionPointer(ty1),_) -> false (* not enough info *)
142 | (_,FunctionPointer(ty2)) -> false (* not enough info *)
143 | (Array(ty1),Array(ty2)) -> loop(ty1,ty2)
144 | (_,_) -> t1=t2 in
145 loop (t1,t2)