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