00001 #ifndef ENVIRONMENT_HXX
00002 #define ENVIRONMENT_HXX
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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <iostream>
00042 #include <map>
00043 #include <string>
00044 #include <set>
00045
00046 #include "shared_ptr.hxx"
00047
00048
00049
00050
00051
00052
00053
00054 #define BF_PRIVATE 0x1u
00055 #define BF_COMPLETE 0x2u
00056 #define BF_REBIND 0x4u
00057
00058 #define BF_NO_MERGE 0x8u
00059
00060
00061
00062
00063 #define BF_PROVIDING 0x10u
00064
00065
00066 template <class T>
00067 struct Binding {
00068 std::string nm;
00069 boost::shared_ptr<T> val;
00070 unsigned flags;
00071
00072 Binding(const std::string& _nm, boost::shared_ptr<T> _val)
00073 {
00074 nm = _nm;
00075 val = _val;
00076 flags = 0;
00077 }
00078
00079 static inline boost::shared_ptr<Binding>
00080 make (const std::string& _nm, boost::shared_ptr<T> _val) {
00081 Binding *tmp = new Binding(_nm, _val);
00082 return boost::shared_ptr<Binding>(tmp);
00083 }
00084 };
00085
00086 template <class T>
00087 struct Environment
00088 : public boost::enable_shared_from_this<Environment<T> > {
00089 std::string uocName;
00090 boost::shared_ptr<Environment<T> > parent;
00091 boost::shared_ptr<Environment<T> > defEnv;
00092
00093 typedef typename std::map<std::string, boost::shared_ptr<Binding<T> > >::iterator iterator;
00094 typedef typename std::map<std::string, boost::shared_ptr<Binding<T> > >::const_iterator const_iterator;
00095 std::map<std::string, boost::shared_ptr<Binding<T> > > bindings;
00096
00097 private:
00098 boost::shared_ptr<Binding<T> > latest;
00099 public:
00100 boost::shared_ptr<Binding<T> > getLatest() {
00101 return latest;
00102 }
00103
00104 iterator begin() {
00105 return bindings.begin();
00106 }
00107 iterator end() {
00108 return bindings.end();
00109 }
00110 iterator find(std::string s) {
00111 return bindings.find(s);
00112 }
00113 const_iterator find(std::string s) const {
00114 return bindings.find(s);
00115 }
00116 const_iterator begin() const {
00117 return bindings.begin();
00118 }
00119 const_iterator end() const {
00120 return bindings.end();
00121 }
00122 size_t size() const {
00123 return bindings.size();
00124 }
00125
00128 boost::shared_ptr< Binding<T> >
00129 doGetBinding(const std::string& nm,
00130 boost::shared_ptr<Environment<T> > outerLimit
00131 = boost::GC_NULL) const;
00132
00133 boost::shared_ptr< Binding<T> >
00134 getLocalBinding(const std::string& nm) const;
00135
00136 Environment(std::string _uocName)
00137 {
00138 uocName = _uocName;
00139 parent = boost::GC_NULL;
00140 defEnv = boost::GC_NULL;
00141 }
00142
00143 static inline boost::shared_ptr<Environment>
00144 make(std::string _uocName) {
00145 Environment *tmp = new Environment(_uocName);
00146 return boost::shared_ptr<Environment>(tmp);
00147 }
00148
00149 ~Environment();
00150
00151 void addBinding(const std::string& name, boost::shared_ptr<T> val,
00152 bool rebind = false);
00153 void
00154 addDefBinding(const std::string& name, boost::shared_ptr<T> val)
00155 {
00156 defEnv->addBinding(name, val);
00157 }
00158
00159 void removeBinding(const std::string& name);
00160
00161
00162 void updateKey(const std::string& from, const std::string& to);
00163
00164 inline boost::shared_ptr<T>
00165 getBinding(const std::string& nm,
00166 boost::shared_ptr<Environment<T> > outerLimit
00167 = boost::GC_NULL) const
00168 {
00169 boost::shared_ptr<const Binding<T> > binding = doGetBinding(nm, outerLimit);
00170 if (binding) return binding->val;
00171 return boost::GC_NULL;
00172 }
00173
00174 inline unsigned
00175 getFlags(const std::string& nm)
00176 {
00177 boost::shared_ptr<const Binding<T> > binding = doGetBinding(nm);
00178 return (binding ? binding->flags : 0);
00179 }
00180
00181 inline void
00182 setFlags(const std::string& nm, unsigned long flags)
00183 {
00184 boost::shared_ptr<Binding<T> > binding = doGetBinding(nm);
00185 if (binding) binding->flags |= flags;
00186 }
00187
00188 void mergeBindingsFrom(boost::shared_ptr<Environment<T> > from, bool complete=true);
00189
00190 boost::shared_ptr<Environment<T> > newScope();
00191
00192 boost::shared_ptr<Environment<T> > newDefScope();
00193
00194
00195 bool isAncestor(boost::shared_ptr<Environment<T> > env);
00196
00197 std::string asString() const;
00198 };
00199
00200
00201
00202
00203
00204
00205
00206
00207 struct Instance;
00208
00209 typedef std::less<boost::shared_ptr<Instance> > InstancePtrLess;
00210
00211 typedef std::set<boost::shared_ptr<Instance>,
00212 InstancePtrLess > InstanceSet;
00213
00214 typedef Environment<InstanceSet> InstEnvironment;
00215
00216 struct AST;
00217 typedef Environment<AST> ASTEnvironment;
00218
00219 struct TypeScheme;
00220 typedef Environment<TypeScheme> TSEnvironment;
00221
00222 #endif