From a3e1d8a5f99d5c12d7cffcbbbcf2bb58830e83c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Pasteur?= Date: Mon, 13 Sep 2010 10:18:52 +0200 Subject: [PATCH] Make sure that idents have unique names Make sure that Idents.name returns two different strings for two different idents. This fixes a problem with variables in two automaton states with the same name (as shown by test/good/name_clash.ept). --- compiler/global/idents.ml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/compiler/global/idents.ml b/compiler/global/idents.ml index bd7c5c1..1092c47 100644 --- a/compiler/global/idents.ml +++ b/compiler/global/idents.ml @@ -81,5 +81,29 @@ module IdentSet = struct Format.fprintf ff "}@]"; end +module S = Set.Make (struct type t = string + let compare = Pervasives.compare end) + +(** @return a unique string for each identifier. Idents corresponding + to variables defined in the source file have the same name unless + there is a collision. *) +let name = + let used_names = ref S.empty in + let env = ref Env.empty in + let rec fresh_string base = + let base = name (fresh base) in + if S.mem base !used_names then fresh_string base else base + in + let unique_name n = + if Env.mem n !env then + Env.find n !env + else + let s = name n in + let s = if S.mem s !used_names then fresh_string s else s in + used_names := S.add s !used_names; + env := Env.add n s !env; + s + in + unique_name let print_ident ff id = Format.fprintf ff "%s" (name id)