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