## lustrec / src / types.ml @ b3f91fdb

History | View | Annotate | Download (10.2 KB)

1 | b38ffff3 | ploc | (********************************************************************) |
---|---|---|---|

2 | (* *) |
||

3 | (* The LustreC compiler toolset / The LustreC Development Team *) |
||

4 | (* Copyright 2012 - -- ONERA - CNRS - INPT - LIFL *) |
||

5 | (* *) |
||

6 | (* LustreC is free software, distributed WITHOUT ANY WARRANTY *) |
||

7 | (* under the terms of the GNU Lesser General Public License *) |
||

8 | (* version 2.1. *) |
||

9 | (* *) |
||

10 | (* This file was originally from the Prelude compiler *) |
||

11 | (* *) |
||

12 | (********************************************************************) |
||

13 | 0cbf0839 | ploc | |

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

15 | open Utils |
||

16 | open Dimension |
||

17 | |||

18 | type type_expr = |
||

19 | {mutable tdesc: type_desc; |
||

20 | tid: int} |
||

21 | |||

22 | and type_desc = |
||

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

24 | | Tint |
||

25 | | Treal |
||

26 | | Tbool |
||

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

28 | in a clock declaration *) |
||

29 | 6b4d172f | xthirioux | | Tclock of type_expr (* A type expression explicitely tagged as carrying a clock *) |

30 | 0cbf0839 | ploc | | Tarrow of type_expr * type_expr |

31 | | Ttuple of type_expr list |
||

32 | | Tenum of ident list |
||

33 | aa223e69 | xthirioux | | Tstruct of (ident * type_expr) list |

34 | 0cbf0839 | ploc | | Tarray of dim_expr * type_expr |

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

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

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

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

39 | |||

40 | type error = |
||

41 | Unbound_value of ident |
||

42 | | Already_bound of ident |
||

43 | | Already_defined of ident |
||

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

45 | 0b78e972 | xthirioux | | Declared_but_undefined of ident |

46 | 0cbf0839 | ploc | | Unbound_type of ident |

47 | | Not_a_dimension |
||

48 | | Not_a_constant |
||

49 | 6b4d172f | xthirioux | | Assigned_constant of ident |

50 | 0cbf0839 | ploc | | WrongArity of int * int |

51 | 14d694c7 | xthirioux | | WrongMorphism of int * int |

52 | 6aeb3388 | xthirioux | | Type_mismatch of ident |

53 | 0cbf0839 | ploc | | Type_clash of type_expr * type_expr |

54 | | Poly_imported_node of ident |
||

55 | |||

56 | exception Unify of type_expr * type_expr |
||

57 | exception Error of Location.t * error |
||

58 | |||

59 | (* Pretty-print*) |
||

60 | open Format |
||

61 | aa223e69 | xthirioux | |

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

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

64 | and print_ty fmt ty = |
||

65 | 0cbf0839 | ploc | match ty.tdesc with |

66 | | Tvar -> |
||

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

68 | | Tint -> |
||

69 | fprintf fmt "int" |
||

70 | | Treal -> |
||

71 | fprintf fmt "real" |
||

72 | | Tbool -> |
||

73 | fprintf fmt "bool" |
||

74 | | Tclock t -> |
||

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

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

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

78 | | Tconst t -> |
||

79 | fprintf fmt "%s" t |
||

80 | | Trat -> |
||

81 | fprintf fmt "rat" |
||

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

83 | f6fa97f9 | xthirioux | fprintf fmt "%a -> %a" print_ty ty1 print_ty ty2 |

84 | 0cbf0839 | ploc | | Ttuple tylist -> |

85 | fprintf fmt "(%a)" |
||

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

87 | | Tenum taglist -> |
||

88 | aa223e69 | xthirioux | fprintf fmt "enum {%a }" |

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

90 | | Tstruct fieldlist -> |
||

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

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

93 | 0cbf0839 | ploc | | Tarray (e, ty) -> |

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

95 | | Tlink ty -> |
||

96 | print_ty fmt ty |
||

97 | | Tunivar -> |
||

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

99 | |||

100 | aa223e69 | xthirioux | let rec print_node_struct_ty_field fmt (label, ty) = |

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

102 | and print_node_ty fmt ty = |
||

103 | 8b3afe43 | xthirioux | match ty.tdesc with |

104 | 0b78e972 | xthirioux | | Tvar -> begin |

105 | 66e38617 | xthirioux | (*Format.eprintf "DEBUG:Types.print_node@.";*) |

106 | 0b78e972 | xthirioux | fprintf fmt "_%s" (name_of_type ty.tid) |

107 | end |
||

108 | 8b3afe43 | xthirioux | | Tint -> |

109 | fprintf fmt "int" |
||

110 | | Treal -> |
||

111 | fprintf fmt "real" |
||

112 | | Tbool -> |
||

113 | fprintf fmt "bool" |
||

114 | | Tclock t -> |
||

115 | 6b4d172f | xthirioux | fprintf fmt "%a clock" print_node_ty t |

116 | 8b3afe43 | xthirioux | | Tstatic (_, t) -> |

117 | fprintf fmt "%a" print_node_ty t |
||

118 | | Tconst t -> |
||

119 | fprintf fmt "%s" t |
||

120 | | Trat -> |
||

121 | fprintf fmt "rat" |
||

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

123 | 6b4d172f | xthirioux | fprintf fmt "%a -> %a" print_node_ty ty1 print_node_ty ty2 |

124 | 8b3afe43 | xthirioux | | Ttuple tylist -> |

125 | fprintf fmt "(%a)" |
||

126 | 6b4d172f | xthirioux | (Utils.fprintf_list ~sep:"*" print_node_ty) tylist |

127 | 8b3afe43 | xthirioux | | Tenum taglist -> |

128 | aa223e69 | xthirioux | fprintf fmt "enum {%a }" |

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

130 | | Tstruct fieldlist -> |
||

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

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

133 | 8b3afe43 | xthirioux | | Tarray (e, ty) -> |

134 | 6b4d172f | xthirioux | fprintf fmt "%a^%a" print_node_ty ty Dimension.pp_dimension e |

135 | 8b3afe43 | xthirioux | | Tlink ty -> |

136 | 6b4d172f | xthirioux | print_node_ty fmt ty |

137 | 8b3afe43 | xthirioux | | Tunivar -> |

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

139 | |||

140 | 0cbf0839 | ploc | let pp_error fmt = function |

141 | | Unbound_value id -> |
||

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

143 | | Unbound_type id -> |
||

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

145 | | Already_bound id -> |
||

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

147 | | Already_defined id -> |
||

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

149 | | Not_a_constant -> |
||

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

151 | 6b4d172f | xthirioux | | Assigned_constant id -> |

152 | fprintf fmt "The constant %s cannot be assigned@." id |
||

153 | 0cbf0839 | ploc | | Not_a_dimension -> |

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

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

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

157 | 14d694c7 | xthirioux | | WrongMorphism (ar1, ar2) -> |

158 | fprintf fmt "Expecting %d argument(s) for homomorphic extension, found %d@." ar1 ar2 |
||

159 | 6aeb3388 | xthirioux | | Type_mismatch id -> |

160 | fprintf fmt "Definition and declaration of type %s don't agree@." id |
||

161 | 0cbf0839 | ploc | | Undefined_var vmap -> |

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

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

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

165 | 0b78e972 | xthirioux | | Declared_but_undefined id -> |

166 | 70e1006b | xthirioux | fprintf fmt "%s is declared but not defined@." id |

167 | 0cbf0839 | ploc | | Type_clash (ty1,ty2) -> |

168 | Utils.reset_names (); |
||

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

170 | | Poly_imported_node id -> |
||

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

172 | |||

173 | |||

174 | let new_id = ref (-1) |
||

175 | |||

176 | let new_ty desc = |
||

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

178 | |||

179 | let new_var () = |
||

180 | new_ty Tvar |
||

181 | |||

182 | let new_univar () = |
||

183 | new_ty Tunivar |
||

184 | |||

185 | let rec repr = |
||

186 | function |
||

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

188 | repr t' |
||

189 | | t -> t |
||

190 | |||

191 | let get_static_value ty = |
||

192 | aa223e69 | xthirioux | match (repr ty).tdesc with |

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

194 | | _ -> None |
||

195 | |||

196 | b3f91fdb | xthirioux | let rec rename_static rename ty = |

197 | match (repr ty).tdesc with |
||

198 | | Tstatic (d, ty') -> { ty with tdesc = Tstatic (Dimension.expr_replace_expr rename d, rename_static rename ty') } |
||

199 | | Tarray (d, ty') -> { ty with tdesc = Tarray (Dimension.expr_replace_expr rename d, rename_static rename ty') } |
||

200 | | _ -> ty |
||

201 | (*Format.eprintf "Types.rename_static %a = %a@." print_ty ty print_ty res; res*) |
||

202 | |||

203 | aa223e69 | xthirioux | let get_field_type ty label = |

204 | match (repr ty).tdesc with |
||

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

206 | | _ -> None |
||

207 | 0cbf0839 | ploc | |

208 | 6b4d172f | xthirioux | let is_numeric_type ty = |

209 | match (repr ty).tdesc with |
||

210 | | Tint |
||

211 | | Treal -> true |
||

212 | | _ -> false |
||

213 | |||

214 | let is_bool_type ty = |
||

215 | match (repr ty).tdesc with |
||

216 | | Tbool -> true |
||

217 | | _ -> false |
||

218 | |||

219 | let get_clock_base_type ty = |
||

220 | c518d082 | xthirioux | match (repr ty).tdesc with |

221 | 6b4d172f | xthirioux | | Tclock ty -> Some ty |

222 | | _ -> None |
||

223 | c518d082 | xthirioux | |

224 | 0cbf0839 | ploc | let rec is_dimension_type ty = |

225 | match (repr ty).tdesc with |
||

226 | | Tint |
||

227 | | Tbool -> true |
||

228 | | Tclock ty' |
||

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

230 | | _ -> false |
||

231 | |||

232 | 14d694c7 | xthirioux | let dynamic_type ty = |

233 | 0cbf0839 | ploc | let ty = repr ty in |

234 | match ty.tdesc with |
||

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

236 | | _ -> ty |
||

237 | |||

238 | 14d694c7 | xthirioux | let is_tuple_type ty = |

239 | match (repr ty).tdesc with |
||

240 | | Ttuple _ -> true |
||

241 | | _ -> false |
||

242 | |||

243 | 0cbf0839 | ploc | let map_tuple_type f ty = |

244 | let ty = dynamic_type ty in |
||

245 | match ty.tdesc with |
||

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

247 | | _ -> f ty |
||

248 | 51768260 | xthirioux | |

249 | d96d54ac | xthirioux | let rec is_struct_type ty = |

250 | 51768260 | xthirioux | match (repr ty).tdesc with |

251 | | Tstruct _ -> true |
||

252 | d96d54ac | xthirioux | | Tstatic (_, ty') -> is_struct_type ty' |

253 | 51768260 | xthirioux | | _ -> false |

254 | |||

255 | 0cbf0839 | ploc | let rec is_array_type ty = |

256 | match (repr ty).tdesc with |
||

257 | | Tarray _ -> true |
||

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

259 | 0cbf0839 | ploc | | _ -> false |

260 | |||

261 | let array_type_dimension ty = |
||

262 | match (dynamic_type ty).tdesc with |
||

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

264 | b3f91fdb | xthirioux | | _ -> (Format.eprintf "internal error: Types.array_type_dimension %a@." print_ty ty; assert false) |

265 | 0cbf0839 | ploc | |

266 | let rec array_type_multi_dimension ty = |
||

267 | match (dynamic_type ty).tdesc with |
||

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

269 | | _ -> [] |
||

270 | |||

271 | let array_element_type ty = |
||

272 | match (dynamic_type ty).tdesc with |
||

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

274 | b3f91fdb | xthirioux | | _ -> (Format.eprintf "internal error: Types.array_element_type %a@." print_ty ty; assert false) |

275 | 0cbf0839 | ploc | |

276 | let rec array_base_type ty = |
||

277 | let ty = repr ty in |
||

278 | match ty.tdesc with |
||

279 | | Tarray (_, ty') |
||

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

281 | | _ -> ty |
||

282 | |||

283 | 51768260 | xthirioux | let is_address_type ty = |

284 | is_array_type ty || is_struct_type ty |
||

285 | |||

286 | 0cbf0839 | ploc | let rec is_generic_type ty = |

287 | match (dynamic_type ty).tdesc with |
||

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

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

290 | | _ -> false |
||

291 | |||

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

293 | (ensured by language syntax) *) |
||

294 | let rec split_arrow ty = |
||

295 | match (repr ty).tdesc with |
||

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

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

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

299 | needs to be considered here *) |
||

300 | 5b5625e1 | ploc | | _ -> Format.eprintf "type %a is not a map@.Unable to split@.@?" print_ty ty; assert false |

301 | 0cbf0839 | ploc | |

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

303 | let type_of_type_list tyl = |
||

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

305 | new_ty (Ttuple tyl) |
||

306 | else |
||

307 | List.hd tyl |
||

308 | |||

309 | let type_list_of_type ty = |
||

310 | match (repr ty).tdesc with |
||

311 | | Ttuple tl -> tl |
||

312 | | _ -> [ty] |
||

313 | |||

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

315 | let rec is_polymorphic ty = |
||

316 | match ty.tdesc with |
||

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

318 | | Tclock ty -> is_polymorphic ty |
||

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

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

321 | aa223e69 | xthirioux | | Tstruct fl -> List.exists (fun (_, t) -> is_polymorphic t) fl |

322 | 0cbf0839 | ploc | | Tlink t' -> is_polymorphic t' |

323 | | Tarray (d, ty) |
||

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

325 | | Tunivar -> true |
||

326 | |||

327 | |||

328 | let mktyptuple nb typ = |
||

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

330 | Ttuple (Array.to_list array) |
||

331 | |||

332 | |||

333 | (* Local Variables: *) |
||

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

335 | (* End: *) |