]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_crate_map.h
a57840ffe09556ef138f009c1ae0d0a2d85c7151
[rust.git] / src / rt / rust_crate_map.h
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #ifndef RUST_CRATE_MAP_H
12 #define RUST_CRATE_MAP_H
13
14 #include "rust_log.h"
15 #include <stdint.h>
16
17 struct mod_entry {
18     const char* name;
19     uint32_t* state;
20 };
21
22 class cratemap;
23
24 class cratemap_v0 {
25     friend class cratemap;
26     const mod_entry *m_entries;
27     const cratemap* m_children[1];
28 };
29
30 class cratemap {
31 private:
32     int32_t m_version;
33     const void *m_annihilate_fn;
34     const mod_entry* m_entries;
35     const cratemap* m_children[1];
36
37     inline int32_t version() const {
38         switch (m_version) {
39         case 1:     return 1;
40         default:    return 0;
41         }
42     }
43
44 public:
45     typedef const cratemap *const *iterator;
46
47     inline const void *annihilate_fn() const {
48         switch (version()) {
49         case 0: return NULL;
50         case 1: return m_annihilate_fn;
51         default: assert(false && "Unknown crate map version!");
52             return NULL; // Appease -Werror=return-type
53         }
54     }
55
56     inline const mod_entry *entries() const {
57         switch (version()) {
58         case 0: return reinterpret_cast<const cratemap_v0 *>(this)->m_entries;
59         case 1: return m_entries;
60         default: assert(false && "Unknown crate map version!");
61             return NULL; // Appease -Werror=return-type
62         }
63     }
64
65     inline const iterator begin() const {
66         switch (version()) {
67         case 0:
68             return &reinterpret_cast<const cratemap_v0 *>(this)->
69                 m_children[0];
70         case 1:
71             return &m_children[1];
72         default: assert(false && "Unknown crate map version!");
73             return NULL; // Appease -Werror=return-type
74         }
75     }
76
77     inline const iterator end() const {
78         iterator i = begin();
79         while (*i)
80             i++;
81         return i;
82     }
83 };
84
85 void iter_module_map(const mod_entry* map,
86                      void (*fn)(const mod_entry* entry, void *cookie),
87                      void *cookie);
88
89 void iter_crate_map(const cratemap* map,
90                     void (*fn)(const mod_entry* entry, void *cookie),
91                     void *cookie);
92
93 //
94 // Local Variables:
95 // mode: C++
96 // fill-column: 78;
97 // indent-tabs-mode: nil
98 // c-basic-offset: 4
99 // buffer-file-coding-system: utf-8-unix
100 // End:
101 //
102
103 #endif /* RUST_CRATE_MAP_H */