95cc325b575758d05330403a1ad3c69dab13cf1e
[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 | IntType | DoubleType
53 | FloatType | LongType | LongLongType | BoolType
54
55 and structUnion = Struct | Union
56
57 and sign = Signed | Unsigned
58
59 and const_vol = Const | Volatile
60
61 (* --------------------------------------------------------------------- *)
62 (* Printer *)
63 open Format
64
65 let rec type2c = function
66 ConstVol(cv,ty) -> (const_vol cv) ^ (type2c ty)
67 | BaseType(ty) -> baseType ty
68 | SignedT(sgn,None) -> sign sgn
69 | SignedT(sgn,Some ty) -> (sign sgn) ^ (type2c ty)
70 | Pointer(ty) -> (type2c ty) ^ "*"
71 | FunctionPointer(ty) -> (type2c ty) ^ "(*)(...)"
72 | Array(ty) -> (type2c ty) ^ "[] "
73 | EnumName(name) -> "enum " ^ (print_name name)
74 | StructUnionName(kind,name) -> (structUnion kind) ^ (print_name name)
75 | TypeName(name) -> name ^ " "
76 | MetaType((rule,name),keep,inherited) -> name ^ " "
77 (*
78 let print_unitary = function
79 Unitary -> print_string "unitary"
80 | Nonunitary -> print_string "nonunitary"
81 | Saved -> print_string "saved" in
82 print_string "/* ";
83 print_string "keep:"; print_unitary keep;
84 print_string " inherited:"; print_bool inherited;
85 print_string " */"
86 *)
87 | Unknown -> "unknown "
88
89 and print_name = function
90 NoName -> ""
91 | MV ((_,name),_,_) -> name ^ " "
92 | Name name -> name ^ " "
93
94 and baseType = function
95 VoidType -> "void "
96 | CharType -> "char "
97 | ShortType -> "short "
98 | IntType -> "int "
99 | DoubleType -> "double "
100 | FloatType -> "float "
101 | LongType -> "long "
102 | LongLongType -> "long long "
103 | BoolType -> "bool "
104
105 and structUnion = function
106 Struct -> "struct "
107 | Union -> "union "
108
109 and sign = function
110 Signed -> "signed "
111 | Unsigned -> "unsigned "
112
113 and const_vol = function
114 Const -> "const "
115 | Volatile -> "volatile "
116
117 let typeC t = print_string (type2c t)
118
119 (* t1 should be less informative than t1, eg t1 = Pointer(Unknown) and t2 =
120 Pointer(int) *)
121 (* only used in iso *)
122 (* needs to do something for MetaType *)
123 let compatible t1 = function
124 None -> t1 = Unknown
125 | Some t2 ->
126 let rec loop = function
127 (Unknown,_) -> true
128 | (ConstVol(cv1,ty1),ConstVol(cv2,ty2)) when cv1 = cv2 ->
129 loop(ty1,ty2)
130 | (Pointer(ty1),Pointer(ty2)) -> loop(ty1,ty2)
131 | (FunctionPointer(ty1),_) -> false (* not enough info *)
132 | (_,FunctionPointer(ty2)) -> false (* not enough info *)
133 | (Array(ty1),Array(ty2)) -> loop(ty1,ty2)
134 | (_,_) -> t1=t2 in
135 loop (t1,t2)