Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / modules.ml @ f9f06e7d

History | View | Annotate | Download (7.6 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 Lustre_types
14
open Corelang
15

    
16
let add_symbol loc msg hashtbl name value =
17
 if Hashtbl.mem hashtbl name
18
 then raise (Error (loc, Error.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, Error.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, Error.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, Error.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, Error.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, Error.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, Error.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, Error.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, Error.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, Error.Already_bound_symbol ("const " ^ name)))
113
    | _       -> assert false
114
  with Not_found -> Hashtbl.add consts_table name value
115

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

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

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

    
153

    
154
let rec load_rec ~is_header imported program =
155
  List.fold_left (fun imported decl ->
156
    match decl.top_decl_desc with
157
    | Node nd -> if is_header then
158
                   raise (Error(decl.top_decl_loc,
159
                                LoadError ("node " ^ nd.node_id ^ " declared in a header file")))  
160
                 else
161
                   (add_node nd.node_id decl; imported)
162
    | ImportedNode ind ->
163
       if is_header then
164
         (add_imported_node ind.nodei_id decl; imported)
165
       else
166
         raise (Error(decl.top_decl_loc,
167
                      LoadError ("imported node " ^ ind.nodei_id ^ " declared in a regular Lustre file")))  
168
    | Const c -> (add_const is_header c.const_id decl; imported)
169
    | TypeDef tdef -> (add_type is_header tdef.tydef_id decl; imported)
170
    | Open (local, dep) ->
171
       let basename = Options_management.name_dependency (local, dep) in
172
       if ISet.mem basename imported then imported else
173
	 let lusic = import_dependency_aux decl.top_decl_loc (local, dep)
174
	 in load_rec ~is_header:true (ISet.add basename imported) lusic.Lusic.contents
175
  ) imported program
176

    
177
(* Iterates through lusi definitions and records them in the hashtbl. Open instructions are evaluated and update these hashtbl as well. node_table/type/table/consts_table *)
178
  
179
let load ~is_header imported program =
180
  try
181
    load_rec ~is_header imported program
182
  with
183
    Corelang.Error (loc, err) as exc -> (
184
      Format.eprintf "Import error: %a%a@."
185
	Error.pp_error_msg err
186
	Location.pp_loc loc;
187
      raise exc
188
    );;