## lustrec / src / types.ml @ 0b78e972

History | View | Annotate | Download (9.07 KB)

1 |
(* ---------------------------------------------------------------------------- |
---|---|

2 |
* SchedMCore - A MultiCore Scheduling Framework |

3 |
* Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE |

4 |
* |

5 |
* This file is part of Prelude |

6 |
* |

7 |
* Prelude is free software; you can redistribute it and/or |

8 |
* modify it under the terms of the GNU Lesser General Public License |

9 |
* as published by the Free Software Foundation ; either version 2 of |

10 |
* the License, or (at your option) any later version. |

11 |
* |

12 |
* Prelude is distributed in the hope that it will be useful, but |

13 |
* WITHOUT ANY WARRANTY ; without even the implied warranty of |

14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

15 |
* Lesser General Public License for more details. |

16 |
* |

17 |
* You should have received a copy of the GNU Lesser General Public |

18 |
* License along with this program ; if not, write to the Free Software |

19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |

20 |
* USA |

21 |
*---------------------------------------------------------------------------- *) |

22 | |

23 |
(** Types definitions and a few utility functions on types. *) |

24 |
open Utils |

25 |
open Dimension |

26 | |

27 |
type type_expr = |

28 |
{mutable tdesc: type_desc; |

29 |
tid: int} |

30 | |

31 |
and type_desc = |

32 |
| Tconst of ident (* type constant *) |

33 |
| Tint |

34 |
| Treal |

35 |
| Tbool |

36 |
| Trat (* Actually unused for now. Only place where it can appear is |

37 |
in a clock declaration *) |

38 |
| Tclock of type_expr (* An type expression explicitely tagged as carrying a clock *) |

39 |
| Tarrow of type_expr * type_expr |

40 |
| Ttuple of type_expr list |

41 |
| Tenum of ident list |

42 |
| Tstruct of (ident * type_expr) list |

43 |
| Tarray of dim_expr * type_expr |

44 |
| Tstatic of dim_expr * type_expr (* a type carried by a dimension expression *) |

45 |
| Tlink of type_expr (* During unification, make links instead of substitutions *) |

46 |
| Tvar (* Monomorphic type variable *) |

47 |
| Tunivar (* Polymorphic type variable *) |

48 | |

49 |
type error = |

50 |
Unbound_value of ident |

51 |
| Already_bound of ident |

52 |
| Already_defined of ident |

53 |
| Undefined_var of (unit IMap.t) |

54 |
| Declared_but_undefined of ident |

55 |
| Unbound_type of ident |

56 |
| Not_a_dimension |

57 |
| Not_a_constant |

58 |
| WrongArity of int * int |

59 |
| Type_clash of type_expr * type_expr |

60 |
| Poly_imported_node of ident |

61 | |

62 |
exception Unify of type_expr * type_expr |

63 |
exception Error of Location.t * error |

64 | |

65 |
(* Pretty-print*) |

66 |
open Format |

67 | |

68 |
let rec print_struct_ty_field fmt (label, ty) = |

69 |
fprintf fmt "%a : %a" pp_print_string label print_ty ty |

70 |
and print_ty fmt ty = |

71 |
match ty.tdesc with |

72 |
| Tvar -> |

73 |
fprintf fmt "_%s" (name_of_type ty.tid) |

74 |
| Tint -> |

75 |
fprintf fmt "int" |

76 |
| Treal -> |

77 |
fprintf fmt "real" |

78 |
| Tbool -> |

79 |
fprintf fmt "bool" |

80 |
| Tclock t -> |

81 |
fprintf fmt "%a clock" print_ty t |

82 |
| Tstatic (d, t) -> |

83 |
fprintf fmt "(%a:%a)" Dimension.pp_dimension d print_ty t |

84 |
| Tconst t -> |

85 |
fprintf fmt "%s" t |

86 |
| Trat -> |

87 |
fprintf fmt "rat" |

88 |
| Tarrow (ty1,ty2) -> |

89 |
fprintf fmt "%a->%a" print_ty ty1 print_ty ty2 |

90 |
| Ttuple tylist -> |

91 |
fprintf fmt "(%a)" |

92 |
(Utils.fprintf_list ~sep:"*" print_ty) tylist |

93 |
| Tenum taglist -> |

94 |
fprintf fmt "enum {%a }" |

95 |
(Utils.fprintf_list ~sep:", " pp_print_string) taglist |

96 |
| Tstruct fieldlist -> |

97 |
fprintf fmt "struct {%a }" |

98 |
(Utils.fprintf_list ~sep:"; " print_struct_ty_field) fieldlist |

99 |
| Tarray (e, ty) -> |

100 |
fprintf fmt "%a^%a" print_ty ty Dimension.pp_dimension e |

101 |
| Tlink ty -> |

102 |
print_ty fmt ty |

103 |
| Tunivar -> |

104 |
fprintf fmt "'%s" (name_of_type ty.tid) |

105 | |

106 |
let rec print_node_struct_ty_field fmt (label, ty) = |

107 |
fprintf fmt "%a : %a" pp_print_string label print_node_ty ty |

108 |
and print_node_ty fmt ty = |

109 |
match ty.tdesc with |

110 |
| Tvar -> begin |

111 |
Format.eprintf "DEBUG:Types.print_node@."; |

112 |
fprintf fmt "_%s" (name_of_type ty.tid) |

113 |
end |

114 |
| Tint -> |

115 |
fprintf fmt "int" |

116 |
| Treal -> |

117 |
fprintf fmt "real" |

118 |
| Tbool -> |

119 |
fprintf fmt "bool" |

120 |
| Tclock t -> |

121 |
fprintf fmt "%a clock" print_ty t |

122 |
| Tstatic (_, t) -> |

123 |
fprintf fmt "%a" print_node_ty t |

124 |
| Tconst t -> |

125 |
fprintf fmt "%s" t |

126 |
| Trat -> |

127 |
fprintf fmt "rat" |

128 |
| Tarrow (ty1,ty2) -> |

129 |
fprintf fmt "%a->%a" print_ty ty1 print_ty ty2 |

130 |
| Ttuple tylist -> |

131 |
fprintf fmt "(%a)" |

132 |
(Utils.fprintf_list ~sep:"*" print_ty) tylist |

133 |
| Tenum taglist -> |

134 |
fprintf fmt "enum {%a }" |

135 |
(Utils.fprintf_list ~sep:", " pp_print_string) taglist |

136 |
| Tstruct fieldlist -> |

137 |
fprintf fmt "struct {%a }" |

138 |
(Utils.fprintf_list ~sep:"; " print_node_struct_ty_field) fieldlist |

139 |
| Tarray (e, ty) -> |

140 |
fprintf fmt "%a^%a" print_ty ty Dimension.pp_dimension e |

141 |
| Tlink ty -> |

142 |
print_ty fmt ty |

143 |
| Tunivar -> |

144 |
fprintf fmt "'%s" (name_of_type ty.tid) |

145 |
| _ -> assert false |

146 | |

147 |
let pp_error fmt = function |

148 |
| Unbound_value id -> |

149 |
fprintf fmt "Unknown value %s@." id |

150 |
| Unbound_type id -> |

151 |
fprintf fmt "Unknown type %s@." id |

152 |
| Already_bound id -> |

153 |
fprintf fmt "%s is already declared@." id |

154 |
| Already_defined id -> |

155 |
fprintf fmt "Multiple definitions of variable %s@." id |

156 |
| Not_a_constant -> |

157 |
fprintf fmt "This expression is not a constant@." |

158 |
| Not_a_dimension -> |

159 |
fprintf fmt "This expression is not a valid dimension@." |

160 |
| WrongArity (ar1, ar2) -> |

161 |
fprintf fmt "Expecting %d argument(s), found %d@." ar1 ar2 |

162 |
| Undefined_var vmap -> |

163 |
fprintf fmt "No definition provided for variable(s): %a@." |

164 |
(Utils.fprintf_list ~sep:"," pp_print_string) |

165 |
(fst (Utils.list_of_imap vmap)) |

166 |
| Declared_but_undefined id -> |

167 |
fprintf fmt "Node %s is declared but not defined@." id |

168 |
| Type_clash (ty1,ty2) -> |

169 |
Utils.reset_names (); |

170 |
fprintf fmt "Expected type %a, got type %a@." print_ty ty1 print_ty ty2 |

171 |
| Poly_imported_node id -> |

172 |
fprintf fmt "Imported nodes cannot have a polymorphic type@." |

173 | |

174 | |

175 |
let new_id = ref (-1) |

176 | |

177 |
let new_ty desc = |

178 |
incr new_id; {tdesc = desc; tid = !new_id } |

179 | |

180 |
let new_var () = |

181 |
new_ty Tvar |

182 | |

183 |
let new_univar () = |

184 |
new_ty Tunivar |

185 | |

186 |
let rec repr = |

187 |
function |

188 |
{tdesc = Tlink t'} -> |

189 |
repr t' |

190 |
| t -> t |

191 | |

192 |
let get_static_value ty = |

193 |
match (repr ty).tdesc with |

194 |
| Tstatic (d, _) -> Some d |

195 |
| _ -> None |

196 | |

197 |
let get_field_type ty label = |

198 |
match (repr ty).tdesc with |

199 |
| Tstruct fl -> (try Some (List.assoc label fl) with Not_found -> None) |

200 |
| _ -> None |

201 | |

202 |
let is_clock_type ty = |

203 |
match (repr ty).tdesc with |

204 |
| Tclock _ -> true |

205 |
| _ -> false |

206 | |

207 |
let rec is_dimension_type ty = |

208 |
match (repr ty).tdesc with |

209 |
| Tint |

210 |
| Tbool -> true |

211 |
| Tclock ty' |

212 |
| Tstatic (_, ty') -> is_dimension_type ty' |

213 |
| _ -> false |

214 | |

215 |
let rec dynamic_type ty = |

216 |
let ty = repr ty in |

217 |
match ty.tdesc with |

218 |
| Tstatic (_, ty') -> ty' |

219 |
| _ -> ty |

220 | |

221 |
let map_tuple_type f ty = |

222 |
let ty = dynamic_type ty in |

223 |
match ty.tdesc with |

224 |
| (Ttuple ty_list) -> { ty with tdesc = Ttuple (List.map f ty_list) } |

225 |
| _ -> f ty |

226 | |

227 |
let rec is_struct_type ty = |

228 |
match (repr ty).tdesc with |

229 |
| Tstruct _ -> true |

230 |
| _ -> false |

231 | |

232 |
let rec is_array_type ty = |

233 |
match (repr ty).tdesc with |

234 |
| Tarray _ -> true |

235 |
| Tstatic (_, ty') -> is_array_type ty' (* looks strange !? *) |

236 |
| _ -> false |

237 | |

238 |
let array_type_dimension ty = |

239 |
match (dynamic_type ty).tdesc with |

240 |
| Tarray (d, _) -> d |

241 |
| _ -> assert false |

242 | |

243 |
let rec array_type_multi_dimension ty = |

244 |
match (dynamic_type ty).tdesc with |

245 |
| Tarray (d, ty') -> d :: array_type_multi_dimension ty' |

246 |
| _ -> [] |

247 | |

248 |
let array_element_type ty = |

249 |
match (dynamic_type ty).tdesc with |

250 |
| Tarray (_, ty') -> ty' |

251 |
| _ -> assert false |

252 | |

253 |
let rec array_base_type ty = |

254 |
let ty = repr ty in |

255 |
match ty.tdesc with |

256 |
| Tarray (_, ty') |

257 |
| Tstatic (_, ty') -> array_base_type ty' |

258 |
| _ -> ty |

259 | |

260 |
let is_address_type ty = |

261 |
is_array_type ty || is_struct_type ty |

262 | |

263 |
let rec is_generic_type ty = |

264 |
match (dynamic_type ty).tdesc with |

265 |
| Tarray (d, ty') -> |

266 |
(not (Dimension.is_dimension_const d)) || (is_generic_type ty') |

267 |
| _ -> false |

268 | |

269 |
(** Splits [ty] into the [lhs,rhs] of an arrow type. Expects an arrow type |

270 |
(ensured by language syntax) *) |

271 |
let rec split_arrow ty = |

272 |
match (repr ty).tdesc with |

273 |
| Tarrow (tin,tout) -> tin,tout |

274 |
| Tstatic (_, ty') -> split_arrow ty' |

275 |
(* Functions are not first order, I don't think the var case |

276 |
needs to be considered here *) |

277 |
| _ -> Format.eprintf "%a@." print_ty ty; assert false |

278 | |

279 |
(** Returns the type corresponding to a type list. *) |

280 |
let type_of_type_list tyl = |

281 |
if (List.length tyl) > 1 then |

282 |
new_ty (Ttuple tyl) |

283 |
else |

284 |
List.hd tyl |

285 | |

286 |
let type_list_of_type ty = |

287 |
match (repr ty).tdesc with |

288 |
| Ttuple tl -> tl |

289 |
| _ -> [ty] |

290 | |

291 |
(** [is_polymorphic ty] returns true if [ty] is polymorphic. *) |

292 |
let rec is_polymorphic ty = |

293 |
match ty.tdesc with |

294 |
| Tenum _ | Tvar | Tint | Treal | Tbool | Trat | Tconst _ -> false |

295 |
| Tclock ty -> is_polymorphic ty |

296 |
| Tarrow (ty1,ty2) -> (is_polymorphic ty1) || (is_polymorphic ty2) |

297 |
| Ttuple tl -> List.exists (fun t -> is_polymorphic t) tl |

298 |
| Tstruct fl -> List.exists (fun (_, t) -> is_polymorphic t) fl |

299 |
| Tlink t' -> is_polymorphic t' |

300 |
| Tarray (d, ty) |

301 |
| Tstatic (d, ty) -> Dimension.is_polymorphic d || is_polymorphic ty |

302 |
| Tunivar -> true |

303 | |

304 | |

305 |
let mktyptuple nb typ = |

306 |
let array = Array.make nb typ in |

307 |
Ttuple (Array.to_list array) |

308 | |

309 | |

310 |
(* Local Variables: *) |

311 |
(* compile-command:"make -C .." *) |

312 |
(* End: *) |