1 | //----------------------------------------------------------------------------- |
---|
2 | /// @file rf_term.hh |
---|
3 | /// |
---|
4 | /// Refal+ term interface |
---|
5 | // |
---|
6 | // $Source$ |
---|
7 | // $Revision: 781 $ |
---|
8 | // $Date: 2003-05-28 17:46:35 +0000 (Wed, 28 May 2003) $ |
---|
9 | //----------------------------------------------------------------------------- |
---|
10 | |
---|
11 | #ifndef __rf_term_hh__ |
---|
12 | #define __rf_term_hh__ |
---|
13 | |
---|
14 | #include "rf_memory_chunk.hh" |
---|
15 | #include "rf_common.hh" |
---|
16 | #include "rf_types.hh" |
---|
17 | #include "rf_object.hh" |
---|
18 | #include "pxx_string.hh" |
---|
19 | |
---|
20 | /// |
---|
21 | /// A namespace for Refal runtime |
---|
22 | namespace rfrt |
---|
23 | { |
---|
24 | |
---|
25 | using namespace rftype ; |
---|
26 | |
---|
27 | // |
---|
28 | // In previous runtime version we keeped the order of memory block in each |
---|
29 | // reference term. This was needed to compute a pointer of a memory block. |
---|
30 | // Now an allocator is able to get this pointer for us, so we don't need all |
---|
31 | // stuff related to this. However, we still keep old code, because in a future |
---|
32 | // we may return to the old scheme if it can lead to faster program execution. |
---|
33 | #if !defined(RF_STORE_CHUNK_ORDER_IN_TERM) |
---|
34 | |
---|
35 | typedef enum { |
---|
36 | term_sym, |
---|
37 | term_ref, |
---|
38 | term_obj, |
---|
39 | term_class_num |
---|
40 | } term_class_t ; |
---|
41 | |
---|
42 | /// |
---|
43 | /// "Flat" bit. This bit, if set for reference term, means that referenced |
---|
44 | /// expression is "flat", i.e. it contains only short symbols, so we should |
---|
45 | /// accurately handle this when destroying the expression. This bit was |
---|
46 | /// introduced because in a case of a "flat" expression we don't need to walk |
---|
47 | /// through and dereference childs. |
---|
48 | /// This bit is kept as invariant during all expression operations. |
---|
49 | #define FLAT_BIT (uintptr_t(0x04)) |
---|
50 | /// |
---|
51 | /// Pointer mask. Used to obtain a clear pointer from term data. |
---|
52 | #define PTR_MASK ~(uintptr_t((sizeof(uintptr_t) << 1) - 1)) |
---|
53 | /// |
---|
54 | /// Type mask. Used to distinguish between reference term and other terms. |
---|
55 | /// This is the maximum number of internal "short" types (i.e. types that can |
---|
56 | /// be placed in place in term data. |
---|
57 | #define TYPE_MASK (uintptr_t(0xFFFF0)) |
---|
58 | #define CLASS_MASK (uintptr_t(0x03)) |
---|
59 | #define TYPE_SHIFT 4 |
---|
60 | |
---|
61 | #else |
---|
62 | #if UINTPTR_MAX == UINT32_MAX |
---|
63 | #define ORDER_SHIFT 3 |
---|
64 | #define REF_BIT (uintptr_t(0x04)) |
---|
65 | #define PTR_MASK (~(uintptr_t(0x07))) |
---|
66 | #define ORDER1_MASK (uintptr_t(0x03)) |
---|
67 | #define ORDER2_MASK (uintptr_t(0x07)) |
---|
68 | #elif UINTPTR_MAX == UINT64_MAX |
---|
69 | #define ORDER_SHIFT 4 |
---|
70 | #define REF_BIT (uintptr_t(0x08)) |
---|
71 | #define PTR_MASK (~(uintptr_t(0x0F))) |
---|
72 | #define ORDER1_MASK (uintptr_t(0x03)) |
---|
73 | #define ORDER2_MASK (uintptr_t(0x0F)) |
---|
74 | #else |
---|
75 | #error "Unsupported pointer size" |
---|
76 | #endif |
---|
77 | //#define COMPOUND_BIT (uintptr_t(0x8000)) |
---|
78 | //#define CHAR_SHIFT ((sizeof(uintptr_t) - sizeof (uint16_t)) * 8) |
---|
79 | #define ORDER1(x) ((uintptr_t)((x) >> ORDER_SHIFT)) |
---|
80 | #define ORDER2(x) ((uintptr_t)((x) & ORDER2_MASK)) |
---|
81 | #define ORDER(x,y) (((x) << ORDER_SHIFT) | (y)) |
---|
82 | |
---|
83 | #endif // RF_STORE_CHUNK_ORDER_IN_TERM |
---|
84 | |
---|
85 | //----------------------------------------------------------------------------- |
---|
86 | /// Refal term. This is one of two basic runtime classes, which describes |
---|
87 | /// Refal expression elements. |
---|
88 | /// |
---|
89 | /// FIXME: To follow all invariants it is necessary to define copy ctor and |
---|
90 | /// assignment operator, which should increment memory block reference counter |
---|
91 | /// in a case of reference term. Destructor should decrement this reference |
---|
92 | /// counter appropriately. This is not done for optimization purposes. |
---|
93 | //----------------------------------------------------------------------------- |
---|
94 | class Term |
---|
95 | { |
---|
96 | /// |
---|
97 | /// We can access all term fields from expression class and some methods |
---|
98 | friend class Expr ; |
---|
99 | friend inline Expr operator + (Term const&, Expr const&) ; |
---|
100 | friend inline Expr operator + (Term const&, Term const&) ; |
---|
101 | friend inline bool flat_eq ( |
---|
102 | Expr const& _e1, uintptr_t _pos1, |
---|
103 | Expr const& _e2, uintptr_t _pos2, uintptr_t _len ); |
---|
104 | |
---|
105 | protected: |
---|
106 | /// |
---|
107 | /// The first term field containing symbol type or (in a case of reference |
---|
108 | /// term) pointer to the first term of referenced expresion |
---|
109 | uintptr_t data1 ; |
---|
110 | /// |
---|
111 | /// The second term field containing actual data (for short type), pointer |
---|
112 | /// to object (for object extensions) or pointer to the right margin of |
---|
113 | /// referenced expression |
---|
114 | |
---|
115 | union { |
---|
116 | uintptr_t uint_data2 ; |
---|
117 | intptr_t int_data2 ; |
---|
118 | void* ptr_data2 ; |
---|
119 | }; |
---|
120 | |
---|
121 | /// |
---|
122 | /// Constructor for empty term with given class and type |
---|
123 | inline Term (term_class_t _class, unsigned _type) ; |
---|
124 | /// |
---|
125 | /// Create expression with specified length |
---|
126 | static inline Expr create_expr (size_t _len, int _align = 0) ; |
---|
127 | /// |
---|
128 | /// Create flat expression with specified length |
---|
129 | static inline Expr create_flat_expr (size_t _len, int _align = 0) ; |
---|
130 | private: |
---|
131 | /// |
---|
132 | /// Constructor for a character term. All constructors are private because |
---|
133 | /// they should be called only from expression implementation. |
---|
134 | /// FIXME: Originally it was planned that all strings in Refal will be |
---|
135 | /// unicode-based (there will be no difference in expression size, because |
---|
136 | /// a term can contain either usual character or wide character). However it |
---|
137 | /// was decided later to have both character types, so probably instead of |
---|
138 | /// this we should have Term (char _c) and Term (wchar_t _c). |
---|
139 | inline Term (uint16_t _c) ; |
---|
140 | /// |
---|
141 | /// Constructor for a reference term. It is is needed for Expr::operator() |
---|
142 | /// implementation. |
---|
143 | /// FIXME: The definition of this method is in rf_expr.hh now. It should be |
---|
144 | /// moved into rf_term.ih, when expression interface and implementation will |
---|
145 | /// be separated. |
---|
146 | inline Term (Expr const& _expr) ; |
---|
147 | inline Term (Object* _obj) ; |
---|
148 | |
---|
149 | inline Term (Term const& _t) ; |
---|
150 | inline Term& operator = (Term const& _t) ; |
---|
151 | |
---|
152 | typedef void (Term::*ctor_func_t)(Term const& _t) ; |
---|
153 | typedef void (Term::*dtor_func_t)() ; |
---|
154 | typedef bool (Term::*eq_func_t)(Term const& _t) const ; |
---|
155 | typedef pxx::WString (Term::*to_string_func_t)() const ; |
---|
156 | typedef size_t (Term::*get_char_len_func_t)() const ; |
---|
157 | typedef Term* (Term::*to_chars_func_t)(Term* _p) const ; |
---|
158 | |
---|
159 | static ctor_func_t ctor_funcs[term_class_num] ; |
---|
160 | static dtor_func_t dtor_funcs[term_class_num] ; |
---|
161 | static eq_func_t eq_funcs[term_class_num] ; |
---|
162 | static to_string_func_t to_string_funcs[term_class_num] ; |
---|
163 | |
---|
164 | void ctor_sym (Term const& _t) ; |
---|
165 | void ctor_ref (Term const& _t) ; |
---|
166 | void ctor_obj (Term const& _t) ; |
---|
167 | void dtor_sym () ; |
---|
168 | void dtor_ref () ; |
---|
169 | void dtor_obj () ; |
---|
170 | bool eq_sym (Term const& _t) const ; |
---|
171 | bool eq_ref (Term const& _t) const ; |
---|
172 | bool eq_obj (Term const& _t) const ; |
---|
173 | pxx::WString to_string_sym () const ; |
---|
174 | pxx::WString to_string_ref () const ; |
---|
175 | pxx::WString to_string_obj () const ; |
---|
176 | |
---|
177 | public: |
---|
178 | /// |
---|
179 | /// Destructor. |
---|
180 | inline ~Term () ; |
---|
181 | /// |
---|
182 | /// Check whether two terms are equal |
---|
183 | inline bool operator == (Term const& _t) const ; |
---|
184 | /// |
---|
185 | /// Check whether two terms are not equal |
---|
186 | inline bool operator != (Term const& _t) const ; |
---|
187 | /// |
---|
188 | /// Check whether two term sequences are equal |
---|
189 | static inline bool eq ( |
---|
190 | Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2 |
---|
191 | ) ; |
---|
192 | /// |
---|
193 | /// Check whether a term is a reference term |
---|
194 | inline bool is_ref () const ; |
---|
195 | /// |
---|
196 | /// Check whether a term is a symbol |
---|
197 | inline bool is_sym () const ; |
---|
198 | /// |
---|
199 | /// Check whether a term is a border symbol |
---|
200 | inline bool is_border () const ; |
---|
201 | /// |
---|
202 | /// Get a pointer to the first term of referenced expression for a reference |
---|
203 | /// term. Note that this method doesn't check whether a term is a reference |
---|
204 | /// term. |
---|
205 | inline Term* get_first () const ; |
---|
206 | /// |
---|
207 | /// Get a pointer to the right margin of referenced expression for |
---|
208 | /// a reference term. Note that this method doesn't checkwhether a term is |
---|
209 | /// a reference term. |
---|
210 | inline Term* get_last () const ; |
---|
211 | /// |
---|
212 | /// Get a type of "short" symbol. |
---|
213 | inline unsigned get_type () const ; |
---|
214 | /// |
---|
215 | /// Set a type of "short" symbol. |
---|
216 | inline void set_type (unsigned _type) ; |
---|
217 | inline term_class_t get_class () const ; |
---|
218 | inline void set_class (term_class_t _class) ; |
---|
219 | inline Object& get_object () ; |
---|
220 | /// |
---|
221 | /// Get a pointer to memory block containing referenced expression. Note |
---|
222 | /// that this method doesn't check whether a term is a reference term. |
---|
223 | inline MemoryChunk* get_mem_chunk() const ; |
---|
224 | |
---|
225 | inline operator pxx::WString () const ; |
---|
226 | |
---|
227 | #if defined(RF_STORE_CHUNK_ORDER_IN_TERM) |
---|
228 | inline unsigned get_order () const |
---|
229 | { |
---|
230 | return ORDER(data1 & ORDER1_MASK, data2 & ORDER2_MASK); |
---|
231 | } |
---|
232 | #endif |
---|
233 | |
---|
234 | }; |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | #endif // __rf_term_hh__ |
---|