Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/lang/ct_struct.h

Go to the documentation of this file.
00001 //  Copyright (C) 2001  Petter Urkedal (petter.urkedal@matfys.lth.se)
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 //
00027 //  $Id: ct_struct.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
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   /** \class ct_incomplete ct_struct.h more/lang/ct_struct.h
00042    **
00043    ** A type which can only be passed as a pointer. */
00044   struct ct_incomplete : ct_type
00045   {
00046       /** Construct a type constructor for an incomplete type with
00047           name \a c_name. */
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       /** Return a clone of the type description.  This should never
00057           be needed for this subclass. */
00058       virtual ct_incomplete* clone() const;
00059 
00060       /** Return the type name provided at construction. */
00061       char const* c_name() const { return m_c_name; }
00062 
00063 #ifdef MORE_LANG_PROVIDE_FFI
00064       /** The \c ffi_type of this \c ct_type. */
00065       virtual ffi_type* ffi_type_of() const;
00066 #endif
00067 
00068       /** Throw an exception. */
00069       virtual void      construct_copy(void*, void*)    const;
00070 
00071       /** Throw an exception. */
00072       virtual void      destruct(void*)                 const;
00073 
00074       /** Throw an exception. */
00075       virtual bool      equal(void*, void*)             const;
00076 
00077       /** Print the type name. */
00078       virtual void print_declaration_pre(std::ostream&, printopt_type) const;
00079 
00080       /** Do nothing. */
00081       virtual void print_declaration_post(std::ostream&, printopt_type) const;
00082 
00083       /** Trow an exception. */
00084       virtual void      hash(hash_type&) const;
00085 
00086       /** True if the type names are equal. */
00087       virtual bool      equal_to(ct_type const*) const;
00088 
00089     private:
00090       char const* m_c_name;
00091   };
00092 
00093   /** \class ct_struct ct_struct.h more/lang/ct_struct.h
00094    **
00095    ** The layout of a store which is of ct_struct type.  Classes are
00096    ** built on top of this. */
00097   struct ct_struct : ct_type
00098   {
00099       /** \class member ct_struct.h more/lang/ct_struct.h
00100        **
00101        ** A descriptor of a member of a structure. */
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           /// The \c ct_type of this member.
00116           ct_type const* type_of()      const { return m_ct_type; }
00117 
00118           /// The \c offset of this member.
00119           /// \pre The \c ct_struct containig this member must be frozen.
00120           size_type     offset()        const { return m_offset; }
00121 
00122           /// The C identifier of this member, if applicable.
00123           char const*   c_name()        const { return m_c_name; }
00124 
00125           /// Return \c p translated \c offset() bytes.
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       /// A bidirectionaly iterator over the range of members.
00148       typedef container::iterator member_iterator;
00149 
00150       /// A bidirectionaly iterator over the range of const members.
00151       typedef container::const_iterator member_const_iterator;
00152 
00153       /** Construct a new struct. Do not provide a name for types to
00154           be defined in a package. */
00155       explicit ct_struct(char const* c_name = 0);
00156 
00157       virtual ~ct_struct();
00158 
00159       /// Return a clone of structure description.
00160       virtual ct_struct* clone() const;
00161 
00162       /// Inform that the description is completed.
00163       void freeze()
00164       {
00165           if (!is_frozen()) finish();
00166       }
00167 
00168       /// True if \c freeze() was called.
00169       bool              is_frozen() const { return m_is_frozen; }
00170 
00171       /// Append a named member to the structure.
00172       member_iterator   append(ct_type const*, char const*);
00173 
00174       /// Append an anonymous member to the structure.
00175       member_iterator   append(ct_type const* t)
00176       {
00177           return append(t, more::gen::struniq_dense(member_count()));
00178       }
00179 
00180       /// Append an inherited type to the structure.  This inheritance
00181       /// is not layout compatible with C++.
00182       member_iterator   append_inherited(ct_type const* t);
00183 
00184       /** Find an inherited type \a t as any subtype of \c this.  If
00185           such a type is fond, return the \c ct_struct which contains
00186           it and its member descriptor.  If the type is ambiguous,
00187           return the uppermost \a ct_struct which contain all
00188           occurances of \a t, and 0 for the member descriptor.  If
00189           there is no such subtype, return zero for both the structure
00190           and member descriptor.
00191           \pre \c this and \a t are not the same. */
00192       std::pair<ct_struct const*, member const*>
00193                         find_inherited_rec(ct_type const* t) const;
00194 
00195       /** Project \a ptr of pointer to \a this type to a pointer to
00196           inherited type \a t.  Return 0 if \a t is not an inherited
00197           type.  */
00198       void*             proj(void* ptr, ct_type const* t) const;
00199 
00200       /// @{
00201       /// The range over members, including inheritance members.
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       /// The number of members, including inheritance members.
00209       size_type         member_count() const { return m_lst.size(); }
00210 
00211       bool              is_optimized() const { return m_do_optimize; }
00212 
00213       /// The C identifier of this type.
00214       identifier        c_name() const { return m_c_name; }
00215 
00216       /// Set the C identifier for this type.
00217       void              set_c_name(identifier id) { m_c_name = id; }
00218 
00219 #ifdef MORE_LANG_PROVIDE_FFI
00220       /// The libffi description of this type.
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       /** Prints a forward declaration as C/C++ source code. */
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       /** Prints the definition as C/C++ source code. For C
00236           print_forward must also be called before print_definition to
00237           create a valid source file.  For both languages it is a good
00238           idea to print all forward declarations first in case a
00239           structure contains a pointer to a type defined after its
00240           definition. */
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

Generated on Sat Sep 7 19:11:17 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.