Skip to content

Commit 6721d7e

Browse files
committed
cparser/Env: testing whether two Env.t define the same types
1 parent f812b3c commit 6721d7e

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎cparser/Elab.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,7 @@ let elab_expr ctx loc env a =
25802580

25812581
let elab_attrib_expr loc env a =
25822582
let (b, env') = elab_expr ctx_constexp loc env a in
2583-
if env' != env then
2583+
if not (Env.same_types env' env) then
25842584
fatal_error loc "cannot define new types within attributes";
25852585
b
25862586

‎cparser/Env.ml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ module IdentMap = struct
5353
let add id data m =
5454
let l = try StringMap.find id.name m with Not_found -> [] in
5555
StringMap.add id.name ((id, data) :: l) m
56+
57+
(* Comparison *)
58+
let compare m1 m2 =
59+
if m1 == m2 then 0 else StringMap.compare Stdlib.compare m1 m2
60+
61+
let compare_named m1 m2 =
62+
if m1 == m2 then 0 else
63+
StringMap.compare Stdlib.compare
64+
(StringMap.remove "" m1) (StringMap.remove "" m2)
5665
end
5766

5867
let gensym = ref 0
@@ -320,6 +329,18 @@ let set_builtins blt =
320329
let is_builtin name =
321330
ident_is_bound !Init.env name
322331

332+
(* Do two environments define the same struct/union/enum types? *)
333+
334+
let same_types env1 env2 =
335+
env1 == env2
336+
|| (IdentMap.compare env1.env_tag env2.env_tag = 0
337+
&& IdentMap.compare env1.env_enum env2.env_enum = 0)
338+
339+
let same_named_types env1 env2 =
340+
env1 == env2
341+
|| (IdentMap.compare_named env1.env_tag env2.env_tag = 0
342+
&& IdentMap.compare_named env1.env_enum env2.env_enum = 0)
343+
323344
(* Error reporting *)
324345

325346
open Printf

‎cparser/Env.mli‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ val initial_identifiers: unit -> C.ident list
8686
val initial_declarations: unit -> C.globdecl list
8787
val set_builtins: C.builtins -> unit
8888
val is_builtin : string -> bool
89+
90+
(* Comparing two environments. One of the environments is assumed
91+
to have been obtained from the other environment by a sequence
92+
of zero, one or several [add_*] operations. *)
93+
94+
val same_types: t -> t -> bool
95+
val same_named_types: t -> t -> bool

0 commit comments

Comments
 (0)