aboutsummaryrefslogtreecommitdiffhomepage
path: root/kernel/names.ml
blob: dc1350fa39612b9bd6c2b866a041b04b02a06def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

(* $Id$ *)

open Pp
open Util

(* Identifiers *)

type identifier = {
  atom : string ; 
  index : int }

type name = Name of identifier | Anonymous

let make_ident sa n = { atom = sa; index = n }
let repr_ident { atom = sa; index = n } = (sa,n)

let string_of_id { atom = s; index = n } =
  s ^ (if n = -1 then "" else string_of_int n)

let code_of_0 = Char.code '0'
let code_of_9 = Char.code '9'

let id_of_string s =
  let slen = String.length s in
  (* [n'] is the position of the first non nullary digit *)
  let rec numpart n n' =
    if n = 0 then 
      failwith ("identifier " ^ s ^ " cannot be split")
    else 
      let c = Char.code (String.get s (n-1)) in
      if c = code_of_0 && n <> slen then 
	numpart (n-1) n' 
      else if code_of_0 <= c && c <= code_of_9 then 
	numpart (n-1) (n-1)
      else 
	n'
  in
  let numstart = numpart slen slen in
  if numstart = slen then 
    { atom = s; index = -1 } 
  else
    { atom = String.sub s 0 numstart;
      index = int_of_string (String.sub s numstart (slen - numstart)) }

let atompart_of_id id = id.atom
let index_of_id id = id.index

let explode_id { atom = s; index = n } =
  (explode s) @ (if n = -1 then [] else explode (string_of_int n))

let print_id { atom = a; index = n } = 
  match (a,n) with
    | ("",-1) -> [< 'sTR"[]" >]
    | ("",n)  -> [< 'sTR"[" ; 'iNT n ; 'sTR"]" >]
    | (s,n)   -> [< 'sTR s ; (if n = (-1) then [< >] else [< 'iNT n >]) >]

let print_idl idl = prlist_with_sep pr_spc print_id idl

let id_ord id1 id2 =
  let (s1,n1) = repr_ident id1
  and (s2,n2) = repr_ident id2 in
  let s_bit = Pervasives.compare s1 s2 in
  if s_bit = 0 then n1 - n2 else s_bit

let id_without_number id = id.index = (-1)

let first_char id =
  if id.atom = "" then
    failwith "lowercase_first_char"
  else
    String.make 1 id.atom.[0]

module IdOrdered = 
  struct
    type t = identifier
    let compare = id_ord
  end

module Idset = Set.Make(IdOrdered)
module Idmap = Map.Make(IdOrdered)


(* Fresh names *)

let lift_ident { atom = str; index = i } = { atom = str; index = i+1 }

let next_ident_away ({atom=str} as id) avoid = 
  let rec name_rec i =
    let create = if i = (-1) then id else {atom=str;index=i} in
    if List.mem create avoid then name_rec (i+1) else create
  in 
  name_rec (-1)

let rec next_ident_away_from {atom=str;index=i} avoid = 
  let rec name_rec i =
    let create = {atom=str;index=i} in
    if List.mem create avoid then name_rec (i+1) else create
  in 
  name_rec i 

let next_name_away_with_default default name l = 
  match name with
    | Name(str) -> next_ident_away str l
    | Anonymous -> next_ident_away (id_of_string default) l

let next_name_away name l = 
  match name with
    | Name(str) -> next_ident_away str l
    | Anonymous -> id_of_string "_"

(* returns lids@[i1..in] where i1..in are new identifiers prefixed id *)
let get_new_ids n id lids  =
  let rec get_rec n acc =
    if n = 0 then 
      acc 
    else 
      let nid = next_ident_away id (acc@lids) in
      get_rec (n-1) (nid::acc) 
  in 
  get_rec n []

let id_of_name default = function
  | Name s -> s
  | Anonymous -> default

(* Kinds *)

type path_kind = CCI | FW | OBJ

let string_of_kind = function
  | CCI -> "cci" 
  | FW -> "fw" 
  | OBJ -> "obj"

let kind_of_string = function
  | "cci" -> CCI 
  | "fw" -> FW 
  | "obj" -> OBJ
  | _ -> invalid_arg "kind_of_string"


(* Section paths *)

type section_path = {
  dirpath : string list ;
  basename : identifier ;
  kind : path_kind }

let make_path pa id k = { dirpath = pa; basename = id; kind = k }
let repr_path { dirpath = pa; basename = id; kind = k} = (pa,id,k)

let kind_of_path sp = sp.kind
let basename sp = sp.basename
let dirpath sp = sp.dirpath

let string_of_path_mind sp id =
  let (sl,_,k) = repr_path sp in
  String.concat ""
    ((List.flatten (List.map (fun s -> ["#";s]) sl))
     @ [ "#"; string_of_id id; "."; string_of_kind k ])
    
let string_of_path sp = string_of_path_mind sp sp.basename

let path_of_string s =
  try
    let (sl,s,k) = parse_section_path s in
    make_path sl (id_of_string s) (kind_of_string k)
  with
    | Invalid_argument _ -> invalid_arg "path_of_string"

let print_sp sp = [< 'sTR (string_of_path sp) >]


let coerce_path k { dirpath = p; basename = id } =
  { dirpath = p; basename = id; kind = k }

let ccisp_of_fwsp = function
  | { dirpath = p; basename = id; kind = FW } -> 
      { dirpath = p; basename = id; kind = CCI }
  | _ -> invalid_arg "ccisp_of_fwsp"

let ccisp_of { dirpath = p; basename = id } =
  { dirpath = p; basename = id; kind = CCI }

let objsp_of { dirpath = p; basename = id } =
  { dirpath = p; basename = id; kind = OBJ }

let fwsp_of_ccisp = function
  | { dirpath = p; basename = id; kind = CCI } -> 
      { dirpath = p; basename = id; kind = FW }
  | _ -> invalid_arg "fwsp_of_ccisp"

let fwsp_of { dirpath = p; basename = id } =
  { dirpath = p; basename = id; kind = FW }

let append_to_path sp str =
  let (sp,id,k) = repr_path sp in
  make_path sp (id_of_string ((string_of_id id)^str)) k

let sp_ord sp1 sp2 =
  let (p1,id1,k) = repr_path sp1
  and (p2,id2,k') = repr_path sp2 in
  let ck = compare k k' in
  if ck = 0 then
    let p_bit = compare p1 p2 in
    if p_bit = 0 then id_ord id1 id2 else p_bit
  else
    ck

let sp_gt (sp1,sp2) = sp_ord sp1 sp2 > 0

module SpOrdered =
  struct
    type t = section_path
    let compare = sp_ord
  end

module Spset = Set.Make(SpOrdered)

module Spmap = Map.Make(SpOrdered)

(* Hash-consing of name objects *)
module Hident = Hashcons.Make(
  struct 
    type t = identifier
    type u = string -> string
    let hash_sub hstr id =
      { atom = hstr id.atom; index = id.index }
    let equal id1 id2 = id1.atom == id2.atom && id1.index = id2.index
    let hash = Hashtbl.hash
  end)

module Hname = Hashcons.Make(
  struct 
    type t = name
    type u = identifier -> identifier
    let hash_sub hident = function
      | Name id -> Name (hident id)
      | n -> n
    let equal n1 n2 =
      match (n1,n2) with
	| (Name id1, Name id2) -> id1 == id2
        | (Anonymous,Anonymous) -> true
        | _ -> false
    let hash = Hashtbl.hash
  end)

module Hsp = Hashcons.Make(
  struct 
    type t = section_path
    type u = (identifier -> identifier) * (string -> string)
    let hash_sub (hident,hstr) sp =
      { dirpath = List.map hstr sp.dirpath;
        basename = hident sp.basename;
        kind = sp.kind }
    let equal sp1 sp2 =
      (sp1.basename == sp2.basename) && (sp1.kind = sp2.kind)
      && (List.length sp1.dirpath = List.length sp2.dirpath)
      && List.for_all2 (==) sp1.dirpath sp2.dirpath
    let hash = Hashtbl.hash
  end)

let hcons_names () =
  let hstring = Hashcons.simple_hcons Hashcons.Hstring.f () in
  let hident = Hashcons.simple_hcons Hident.f hstring in
  let hname = Hashcons.simple_hcons Hname.f hident in
  let hspcci = Hashcons.simple_hcons Hsp.f (hident,hstring) in
  let hspfw = Hashcons.simple_hcons Hsp.f (hident,hstring) in
  (hspcci,hspfw,hname,hident,hstring)