Project

General

Profile

Download (7.85 KB) Statistics
| Branch: | Tag: | Revision:
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, 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
let rec load_header_rec imported header =
154
  List.fold_left (fun imported decl ->
155
    match decl.top_decl_desc with
156
    | Node nd -> assert false
157
    | ImportedNode ind -> (add_imported_node ind.nodei_id decl; imported)
158
    | Const c -> (add_const true c.const_id decl; imported)
159
    | TypeDef tdef -> (add_type true tdef.tydef_id decl; imported)
160
    | Open (local, dep) ->
161
       let basename = Options_management.name_dependency (local, dep) in
162
       if ISet.mem basename imported then imported else
163
	 let lusic = import_dependency_aux decl.top_decl_loc (local, dep)
164
	 in load_header_rec (ISet.add basename imported) lusic.Lusic.contents
165
		 ) imported header
166

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

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

    
192
let load_program imported program =
193
  try
194
    load_program_rec imported program
195
  with
196
    Corelang.Error (loc, err) as exc -> (
197
      Format.eprintf "Import error: %a%a@."
198
	Error.pp_error_msg err
199
	Location.pp_loc loc;
200
      raise exc
201
    );;
(36-36/61)