00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef MORE_LANG_CT_STRUCT_H
00031 #define MORE_LANG_CT_STRUCT_H
00032
00033 #include <more/lang/ct_type.h>
00034 #include <more/gen/string.h>
00035 #include <stdexcept>
00036 #include <list>
00037
00038 namespace more {
00039 namespace lang {
00040
00041
00042
00043
00044 struct ct_incomplete : ct_type
00045 {
00046
00047
00048 explicit ct_incomplete(char const* c_name)
00049 #ifdef MORE_LANG_USE_FFI
00050 : m_c_name(c_name) {}
00051 #else
00052 : ct_type(0, 0),
00053 m_c_name(c_name) {}
00054 #endif
00055
00056
00057
00058 virtual ct_incomplete* clone() const;
00059
00060
00061 char const* c_name() const { return m_c_name; }
00062
00063 #ifdef MORE_LANG_PROVIDE_FFI
00064
00065 virtual ffi_type* ffi_type_of() const;
00066 #endif
00067
00068
00069 virtual void construct_copy(void*, void*) const;
00070
00071
00072 virtual void destruct(void*) const;
00073
00074
00075 virtual bool equal(void*, void*) const;
00076
00077
00078 virtual void print_declaration_pre(std::ostream&, printopt_type) const;
00079
00080
00081 virtual void print_declaration_post(std::ostream&, printopt_type) const;
00082
00083
00084 virtual void hash(hash_type&) const;
00085
00086
00087 virtual bool equal_to(ct_type const*) const;
00088
00089 private:
00090 char const* m_c_name;
00091 };
00092
00093
00094
00095
00096
00097 struct ct_struct : ct_type
00098 {
00099
00100
00101
00102 struct member
00103 {
00104 typedef std::size_t size_type;
00105
00106 static const size_type no_offset = (size_type)-1;
00107 private:
00108 member() {}
00109 member(ct_type const* t, char const* c_name, member* link = 0)
00110 : m_ct_type(t),
00111 m_c_name(c_name),
00112 m_base_link(link) {}
00113
00114 public:
00115
00116 ct_type const* type_of() const { return m_ct_type; }
00117
00118
00119
00120 size_type offset() const { return m_offset; }
00121
00122
00123 char const* c_name() const { return m_c_name; }
00124
00125
00126 void* apply(void* p) const { return (char*)p + m_offset; }
00127
00128 private:
00129 ct_type const* m_ct_type;
00130 char const* m_c_name;
00131 size_type m_offset;
00132 member* m_base_link;
00133
00134 friend class ct_struct;
00135 };
00136
00137 private:
00138 #ifdef MORE_LANG_USE_GC
00139 typedef std::list< member, more::gen::gc_allocator<member> > container;
00140 #else
00141 typedef std::list<member> container;
00142 #endif
00143
00144 public:
00145 typedef std::size_t size_type;
00146
00147
00148 typedef container::iterator member_iterator;
00149
00150
00151 typedef container::const_iterator member_const_iterator;
00152
00153
00154
00155 explicit ct_struct(char const* c_name = 0);
00156
00157 virtual ~ct_struct();
00158
00159
00160 virtual ct_struct* clone() const;
00161
00162
00163 void freeze()
00164 {
00165 if (!is_frozen()) finish();
00166 }
00167
00168
00169 bool is_frozen() const { return m_is_frozen; }
00170
00171
00172 member_iterator append(ct_type const*, char const*);
00173
00174
00175 member_iterator append(ct_type const* t)
00176 {
00177 return append(t, more::gen::struniq_dense(member_count()));
00178 }
00179
00180
00181
00182 member_iterator append_inherited(ct_type const* t);
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 std::pair<ct_struct const*, member const*>
00193 find_inherited_rec(ct_type const* t) const;
00194
00195
00196
00197
00198 void* proj(void* ptr, ct_type const* t) const;
00199
00200
00201
00202 member_iterator member_begin() { return m_lst.begin(); }
00203 member_iterator member_end() { return m_lst.end(); }
00204 member_const_iterator member_begin() const { return m_lst.begin(); }
00205 member_const_iterator member_end() const { return m_lst.end(); }
00206
00207
00208
00209 size_type member_count() const { return m_lst.size(); }
00210
00211 bool is_optimized() const { return m_do_optimize; }
00212
00213
00214 identifier c_name() const { return m_c_name; }
00215
00216
00217 void set_c_name(identifier id) { m_c_name = id; }
00218
00219 #ifdef MORE_LANG_PROVIDE_FFI
00220
00221 virtual ffi_type* ffi_type_of() const;
00222 #endif
00223 virtual void construct_copy(void*, void*) const;
00224 virtual void destruct(void*) const;
00225 virtual bool equal(void*, void*) const;
00226 virtual void hash(hash_type&) const;
00227 virtual bool equal_to(ct_type const*) const;
00228
00229
00230 void print_forward(std::ostream&, printopt_type=0) const;
00231
00232 virtual void print_declaration_pre(std::ostream&, printopt_type) const;
00233 virtual void print_declaration_post(std::ostream&, printopt_type) const;
00234
00235
00236
00237
00238
00239
00240
00241 void print_definition(std::ostream&, printopt_type=0) const;
00242
00243 private:
00244 void finish();
00245 void req_not_frozen() const;
00246 void req_frozen() const
00247 {
00248 if (!is_frozen())
00249 throw std::logic_error("more::lang::ct_struct: Is not frozen.");
00250 }
00251
00252 identifier m_c_name;
00253 container m_lst;
00254 member* m_base_link;
00255 unsigned int m_is_frozen : 1;
00256 unsigned int m_do_optimize : 1;
00257 #ifdef MORE_LANG_PROVIDE_FFI
00258 mutable ffi_type m_ffi_type;
00259 #endif
00260
00261 static size_type const s_capacity_init = 32;
00262 };
00263
00264 }}
00265 #endif