hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
obj.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_LANG_TYPEPREDS_OBJ_HPP_INCLUDED
3 #define HOBBES_LANG_TYPEPREDS_OBJ_HPP_INCLUDED
4 
7 #include <hobbes/util/str.H>
8 #include <memory>
9 #include <map>
10 #include <string>
11 #include <stdexcept>
12 #include <typeinfo>
13 #include <cxxabi.h>
14 
15 namespace hobbes {
16 
17 // encodes an adjustment to take one class type to another
18 // there are three cases:
19 // * the identity adjustment (for A <: B, A : B, ...; it's safe to treat A as a B)
20 // * the constant adjustment (for A <: B, A : ..., B; the memory for B is at an offset from A)
21 // * the vtbl lookup adjustment (for A <: B, A : virtual B; A holds a pointer to the base address for B)
22 struct PtrAdjustment {
23  PtrAdjustment(bool vtblLookup, int offset, const std::string& targetTy) : vtblLookup(vtblLookup), offset(offset), targetTy(targetTy) { }
24 
25  static PtrAdjustment id(const std::string& tty) { return PtrAdjustment(false, 0, tty); }
26  static PtrAdjustment by(int x, const std::string& tty) { return PtrAdjustment(false, x, tty); }
27  static PtrAdjustment vtbl(int x, const std::string& tty) { return PtrAdjustment(true, x, tty); }
28 
29  bool vtblLookup;
30  int offset;
31  std::string targetTy;
32 
33  std::string show() const {
34  if (this->vtblLookup) {
35  return "vtbl(" + str::from(this->offset) + ")";
36  } else {
37  return "+" + str::from(this->offset);
38  }
39  }
40 };
41 typedef std::vector<PtrAdjustment> PtrAdjustmentPath; // for A <: ... <: Z, all adjustments
42 
43 std::string show(const PtrAdjustmentPath& p);
44 
45 // object relations capture C++ (nominal) subtyping
46 // (this implementation only works with GCC)
47 class Objs : public SubtypeEliminator {
48 public:
49 #ifndef __clang__
50  typedef __cxxabiv1::__class_type_info class_type;
51  typedef __cxxabiv1::__si_class_type_info si_class_type;
52  typedef __cxxabiv1::__vmi_class_type_info vmi_class_type;
53 
54  // record class definitions
55  void add(const class_type* ct);
56 #else
57  typedef int class_type;
58 #endif
59  bool add(const std::type_info& ti);
60  bool add(const std::type_info* ti);
61 
62  template <typename T>
63  bool add() {
64  return add(typeid(T));
65  }
66 
67  bool isObjName(const std::string& tn) const;
68  bool isObjType(const MonoTypePtr& mt) const;
69 
70  // how to adjust one object pointer to the other (assuming that it's possible)
71  PtrAdjustmentPath adjustment(const std::string& derived, const std::string& base) const;
72  PtrAdjustmentPath adjustment(const MonoTypePtr& derived, const MonoTypePtr& base) const;
73  PtrAdjustmentPath adjustment(const ConstraintPtr& cst) const;
74 
75  // add subtyping constraints where valid objects appear
76  PolyTypePtr generalize(const MonoTypePtr& mt) const;
77 
78  // subtype eliminator interface
79  bool refine (const TEnvPtr& tenv, const MonoTypePtr& lhs, const MonoTypePtr& rhs, MonoTypeUnifier* s);
80  bool satisfied (const TEnvPtr& tenv, const MonoTypePtr& lhs, const MonoTypePtr& rhs) const;
81  bool satisfiable(const TEnvPtr& tenv, const MonoTypePtr& lhs, const MonoTypePtr& rhs) const;
82 
83  ExprPtr unqualify(const TEnvPtr&,const ConstraintPtr&,const ExprPtr&,Definitions*) const;
84  PolyTypePtr lookup (const std::string& vn) const;
85  SymSet bindings () const;
86 private:
87  typedef std::map<std::string, const class_type*> ClassDefs;
88  ClassDefs classDefs;
89 
90  bool mayBeKnown(const MonoTypePtr& mt) const;
91  bool pathExists(const std::string& from, const std::string& to) const;
92 };
93 typedef std::shared_ptr<Objs> ObjsPtr;
94 
95 }
96 
97 #endif
Definition: obj.H:22
std::shared_ptr< PolyType > PolyTypePtr
Definition: type.H:23
std::string targetTy
Definition: obj.H:31
PolyTypePtr generalize(const QualTypePtr &qt)
Definition: type.C:2016
__cxxabiv1::__vmi_class_type_info vmi_class_type
Definition: obj.H:52
bool satisfied(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
bool satisfiable(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
std::set< std::string > SymSet
Definition: tyunqualify.H:12
std::map< std::string, const class_type * > ClassDefs
Definition: obj.H:87
bool vtblLookup
Definition: obj.H:29
std::string from(const T &x)
Definition: str.H:101
static PtrAdjustment vtbl(int x, const std::string &tty)
Definition: obj.H:27
Definition: boot.H:7
static PtrAdjustment by(int x, const std::string &tty)
Definition: obj.H:26
static PtrAdjustment id(const std::string &tty)
Definition: obj.H:25
MonoType::ptr MonoTypePtr
Definition: type.H:71
std::vector< PtrAdjustment > PtrAdjustmentPath
Definition: obj.H:41
int offset
Definition: obj.H:30
Definition: typeinf.H:29
Definition: subtype.H:17
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
T to(const std::string &x)
Definition: str.H:79
std::shared_ptr< TEnv > TEnvPtr
Definition: type.H:80
__cxxabiv1::__si_class_type_info si_class_type
Definition: obj.H:51
ClassDefs classDefs
Definition: obj.H:88
std::vector< Definition > Definitions
Definition: expr.H:62
PtrAdjustment(bool vtblLookup, int offset, const std::string &targetTy)
Definition: obj.H:23
std::shared_ptr< Constraint > ConstraintPtr
Definition: type.H:33
bool refine(const TEnvPtr &tenv, const ConstraintPtr &c, MonoTypeUnifier *s, Definitions *)
Definition: typeinf.C:485
std::shared_ptr< Objs > ObjsPtr
Definition: obj.H:93
std::string show() const
Definition: obj.H:33
Definition: obj.H:47
T lookup(const std::map< K, T > &tenv, const K &n)
Definition: cc.C:518
bool add()
Definition: obj.H:63
__cxxabiv1::__class_type_info class_type
Definition: obj.H:50