Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / modules.ml @ a28d1ba7

History | View | Annotate | Download (7.79 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
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
(********************************************************************)
11

    
12
open Utils
13
open LustreSpec
14
open Corelang
15

    
16
let add_symbol loc msg hashtbl name value =
17
 if Hashtbl.mem hashtbl name
18
 then raise (Error (loc, Already_bound_symbol msg))
19
 else Hashtbl.add hashtbl name value
20

    
21
let check_symbol loc msg hashtbl name =
22
 if not (Hashtbl.mem hashtbl name)
23
 then raise (Error (loc, Unbound_symbol msg))
24
 else ()
25

    
26
let add_imported_node name value =
27
(*Format.eprintf "add_imported_node %s %a (owner=%s)@." name Printers.pp_imported_node (imported_node_of_top value) value.top_decl_owner;*)
28
  try
29
    let value' = Hashtbl.find node_table name in
30
    let owner' = value'.top_decl_owner in
31
    let itf' = value'.top_decl_itf in
32
    let owner = value.top_decl_owner in
33
    let itf = value.top_decl_itf in
34
    match value'.top_decl_desc, value.top_decl_desc with
35
    | Node _        , ImportedNode _  when owner = owner' && itf' && (not itf) -> Hashtbl.add node_table name value
36
    | ImportedNode _, ImportedNode _            -> raise (Error (value.top_decl_loc, Already_bound_symbol ("node " ^ name)))
37
    | _                                         -> assert false
38
  with
39
    Not_found                                   -> Hashtbl.add node_table name value
40

    
41
let add_node name value =
42
(*Format.eprintf "add_node %s %a (owner=%s)@." name Printers.pp_imported_node (get_node_interface (node_of_top value)) value.top_decl_owner;*)
43
  try
44
    let value' = Hashtbl.find node_table name in
45
    let owner' = value'.top_decl_owner in
46
    let itf' = value'.top_decl_itf in
47
    let owner = value.top_decl_owner in
48
    let itf = value.top_decl_itf in
49
    match value'.top_decl_desc, value.top_decl_desc with
50
    | ImportedNode _, Node _          when owner = owner' && itf' && (not itf) -> ()
51
    | Node _        , Node _                    -> raise (Error (value.top_decl_loc, Already_bound_symbol ("node " ^ name)))
52
    | _                                         -> assert false
53
  with
54
    Not_found                                   -> Hashtbl.add node_table name value
55

    
56

    
57
let add_tag loc name typ =
58
  if Hashtbl.mem tag_table name then
59
    raise (Error (loc, Already_bound_symbol ("enum tag " ^ name)))
60
  else Hashtbl.add tag_table name typ
61

    
62
let add_field loc name typ =
63
  if Hashtbl.mem field_table name then
64
    raise (Error (loc, Already_bound_symbol ("struct field " ^ name)))
65
  else Hashtbl.add field_table name typ
66

    
67
let import_typedef name tydef =
68
  let loc = tydef.top_decl_loc in
69
  let rec import ty =
70
    match ty with
71
    | Tydec_enum tl   ->
72
       List.iter (fun tag -> add_tag loc tag tydef) tl
73
    | Tydec_struct fl -> 
74
       List.iter (fun (field, ty) -> add_field loc field tydef; import ty) fl
75
    | Tydec_clock ty      -> import ty
76
    | Tydec_const c       ->
77
       if not (Hashtbl.mem type_table (Tydec_const c))
78
       then raise (Error (loc, Unbound_symbol ("type " ^ c)))
79
       else ()
80
    | Tydec_array (c, ty) -> import ty
81
    | _                   -> ()
82
  in import ((typedef_of_top tydef).tydef_desc)
83

    
84
let add_type itf name value =
85
(*Format.eprintf "Modules.add_type %B %s %a (owner=%s)@." itf name Printers.pp_typedef (typedef_of_top value) value.top_decl_owner;*)
86
  try
87
    let value' = Hashtbl.find type_table (Tydec_const name) in
88
    let owner' = value'.top_decl_owner in
89
    let itf' = value'.top_decl_itf in
90
    let owner = value.top_decl_owner in
91
    let itf = value.top_decl_itf in
92
    match value'.top_decl_desc, value.top_decl_desc with
93
    | TypeDef ty', TypeDef ty when coretype_equal ty'.tydef_desc ty.tydef_desc && owner' = owner && itf' && (not itf) -> ()
94
    | TypeDef ty', TypeDef ty -> raise (Error (value.top_decl_loc, Already_bound_symbol ("type " ^ name)))
95
    | _       -> assert false
96
  with Not_found -> (import_typedef name value; Hashtbl.add type_table (Tydec_const name) value)
97

    
98
let check_type loc name =
99
 if not (Hashtbl.mem type_table (Tydec_const name))
100
 then raise (Error (loc, Unbound_symbol ("type " ^ name)))
101
 else ()
102

    
103
let add_const itf name value =
104
  try
105
    let value' = Hashtbl.find consts_table name in
106
    let owner' = value'.top_decl_owner in
107
    let itf' = value'.top_decl_itf in
108
    let owner = value.top_decl_owner in
109
    let itf = value.top_decl_itf in
110
    match value'.top_decl_desc, value.top_decl_desc with
111
    | Const c', Const c when c.const_value = c'.const_value && owner' = owner && itf' && (not itf) -> ()
112
    | Const c', Const c -> raise (Error (value.top_decl_loc, Already_bound_symbol ("const " ^ name)))
113
    | _       -> assert false
114
  with Not_found -> Hashtbl.add consts_table name value
115

    
116
let name_dependency (local, dep) =
117
  ((if local then !Options.dest_dir else Version.include_path) ^ "/") ^ dep
118

    
119
let import_dependency_aux loc (local, dep) =
120
  let basename = name_dependency (local, dep) in
121
  let extension = ".lusic" in 
122
  try
123
    let lusic = Lusic.read_lusic basename extension in
124
    Lusic.check_obsolete lusic basename;
125
    lusic
126
  with
127
  | Sys_error msg ->
128
    begin
129
      (*Format.eprintf "Error: %s@." msg;*)
130
      raise (Error (loc, Unknown_library basename))
131
    end
132
  | Corelang.Error (_, msg) as exc -> raise (Corelang.Error (loc, msg))
133

    
134
let import_dependency loc (local, dep) =
135
  try
136
    import_dependency_aux loc (local, dep)
137
  with
138
  | Corelang.Error (_, err) as exc -> (
139
    Format.eprintf "Import error: %a%a@."
140
      Corelang.pp_error err
141
      Location.pp_loc loc;
142
    raise exc
143
  )
144

    
145
let check_dependency lusic basename =
146
  try
147
    Lusic.check_obsolete lusic basename
148
  with
149
  | Corelang.Error (loc, err) as exc -> (
150
    Format.eprintf "Import error: %a%a@."
151
      Corelang.pp_error err
152
      Location.pp_loc loc;
153
    raise exc
154
  )
155

    
156
let rec load_header_rec imported header =
157
  List.fold_left (fun imp decl ->
158
    match decl.top_decl_desc with
159
    | Node nd -> assert false
160
    | ImportedNode ind -> (add_imported_node ind.nodei_id decl; imp)
161
    | Const c -> (add_const true c.const_id decl; imp)
162
    | TypeDef tdef -> (add_type true tdef.tydef_id decl; imp)
163
    | Open (local, dep) ->
164
       let basename = name_dependency (local, dep) in
165
       if ISet.mem basename imported then imp else
166
	 let lusic = import_dependency_aux decl.top_decl_loc (local, dep)
167
	 in load_header_rec (ISet.add basename imported) lusic.Lusic.contents
168
		 ) imported header
169

    
170
let load_header imported header =
171
  try
172
    load_header_rec imported header
173
  with
174
    Corelang.Error (loc, err) as exc -> (
175
      Format.eprintf "Import error: %a%a@."
176
	Corelang.pp_error err
177
	Location.pp_loc loc;
178
      raise exc
179
    );;
180

    
181
let rec load_program_rec imported program =
182
  List.fold_left (fun imp decl ->
183
    match decl.top_decl_desc with
184
    | Node nd -> (add_node nd.node_id decl; imp)
185
    | ImportedNode ind -> assert false
186
    | Const c -> (add_const false c.const_id decl; imp)
187
    | TypeDef tdef -> (add_type false tdef.tydef_id decl; imp)
188
    | Open (local, dep) ->
189
       let basename = name_dependency (local, dep) in
190
       if ISet.mem basename imported then imp else
191
	 let lusic = import_dependency_aux decl.top_decl_loc (local, dep)
192
	 in load_header_rec (ISet.add basename imported) lusic.Lusic.contents
193
		 ) imported program
194

    
195
let load_program imported program =
196
  try
197
    load_program_rec imported program
198
  with
199
    Corelang.Error (loc, err) as exc -> (
200
      Format.eprintf "Import error: %a%a@."
201
	Corelang.pp_error err
202
	Location.pp_loc loc;
203
      raise exc
204
    );;