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
00031
00032
00033
00034
00035
00036
00037
00038 #include <assert.h>
00039 #include <stdint.h>
00040 #include <dirent.h>
00041 #include <sstream>
00042
00043 #include "Type.hxx"
00044 #include "TypeScheme.hxx"
00045 #include "Typeclass.hxx"
00046 #include "AST.hxx"
00047 #include "Environment.hxx"
00048 #include "inter-pass.hxx"
00049
00050 using namespace std;
00051 using namespace boost;
00052 using namespace sherpa;
00053
00054 template<class T>
00055 shared_ptr<Binding<T> >
00056 Environment<T>::getLocalBinding(const std::string& nm) const
00057 {
00058 const_iterator itr = bindings.find(nm);
00059 if (itr != bindings.end())
00060 return itr->second;
00061
00062 return GC_NULL;
00063 }
00064
00065 template<class T>
00066 shared_ptr<Binding<T> >
00067 Environment<T>::doGetBinding(const std::string& nm,
00068 shared_ptr<Environment<T> > outerLimit) const
00069 {
00070 shared_ptr<Binding<T> > binding = getLocalBinding(nm);
00071
00072 if (binding)
00073 return binding;
00074
00075 if (parent && parent != outerLimit)
00076 return parent->doGetBinding(nm, outerLimit);
00077
00078 return GC_NULL;
00079 }
00080
00081 template<class T>
00082 std::string
00083 Environment<T>::asString() const
00084 {
00085 std::stringstream ss;
00086
00087 if (parent)
00088 ss << parent->asString();
00089
00090 for (const_iterator itr = begin(); itr != end(); ++itr)
00091 ss << itr->first << ": "
00092 << itr->second->val
00093 << std::endl;
00094
00095 ss << std::endl;
00096
00097 return ss.str();
00098 }
00099
00100 template<class T>
00101 void
00102 Environment<T>::removeBinding(const std::string& nm)
00103 {
00104 iterator itr = bindings.find(nm);
00105 if (itr != bindings.end()) {
00106 bindings.erase(itr);
00107 return;
00108 }
00109
00110 if (parent)
00111 parent->removeBinding(nm);
00112 }
00113
00114 template<class T>
00115 void
00116 Environment<T>::addBinding(const std::string& nm,
00117 shared_ptr<T> val,
00118 bool rebind)
00119 {
00120 if (rebind) {
00121 iterator itr = bindings.find(nm);
00122 if (itr != bindings.end()){
00123 itr->second->val = val;
00124 itr->second->flags = 0;
00125 return;
00126 }
00127 }
00128
00129 bindings[nm] = Binding<T>::make(nm, val);
00130 latest = bindings[nm];
00131 }
00132
00133 template<class T>
00134 void
00135 Environment<T>::updateKey(const std::string& from,
00136 const std::string& to)
00137 {
00138 iterator itr = bindings.find(from);
00139 if (itr != bindings.end()) {
00140 boost::shared_ptr<Binding<T> > b = itr->second;
00141 bindings.erase(itr);
00142 bindings[to] = b;
00143 return;
00144 }
00145
00146 if (parent)
00147 parent->updateKey(from, to);
00148 }
00149
00150 template<class T>
00151 shared_ptr<Environment<T> >
00152 Environment<T>::newScope()
00153 {
00154 shared_ptr<Environment<T> > nEnv = Environment<T>::make(uocName);
00155 nEnv->parent = this->shared_from_this();
00156 nEnv->defEnv = defEnv;
00157
00158 return nEnv;
00159 }
00160
00161 template<class T>
00162 shared_ptr<Environment<T> >
00163 Environment<T>::newDefScope()
00164 {
00165 shared_ptr<Environment<T> > nEnv = newScope();
00166 nEnv->defEnv = nEnv;
00167
00168 return nEnv;
00169 }
00170
00171
00172 template<class T>
00173 bool
00174 Environment<T>::isAncestor(shared_ptr<Environment<T> > env)
00175 {
00176 if (parent == env)
00177 return true;
00178 else if (!parent)
00179 return false;
00180 else
00181 return parent->isAncestor(env);
00182 }
00183
00184 template<class T>
00185 void
00186 Environment<T>::mergeBindingsFrom(shared_ptr<Environment<T> > from, bool complete)
00187 {
00188 for (iterator itr = from->begin();
00189 itr != from->end(); ++itr ) {
00190 const std::string& nm = itr->first;
00191 shared_ptr<T> val = itr->second->val;
00192 unsigned long flags = itr->second->flags;
00193
00194 if (flags & BF_NO_MERGE)
00195 continue;
00196
00197 if (complete)
00198 flags |= BF_COMPLETE;
00199
00200 bool rebind = (flags & BF_REBIND) ? true : false;
00201 flags &= ~BF_REBIND;
00202
00203 addBinding(nm, val, rebind);
00204 setFlags(nm, flags);
00205 }
00206 }
00207
00208
00209 template<class T>
00210 Environment<T>::~Environment()
00211 {
00212 }
00213
00214
00215
00216 template shared_ptr<Binding<AST> >
00217 Environment<AST>::getLocalBinding(const std::string& nm) const;
00218 template shared_ptr<Binding<TypeScheme> >
00219 Environment<TypeScheme>::getLocalBinding(const std::string& nm) const;
00220 template shared_ptr<Binding<InstanceSet> >
00221 InstEnvironment::getLocalBinding
00222 (const std::string& nm) const;
00223
00224 template shared_ptr<Binding<AST> >
00225 Environment<AST>::doGetBinding(const std::string& nm,
00226 shared_ptr<Environment<AST> >) const;
00227 template shared_ptr<Binding<TypeScheme> >
00228 Environment<TypeScheme>::doGetBinding(const std::string& nm,
00229 shared_ptr<Environment<TypeScheme> >) const;
00230 template shared_ptr<Binding<InstanceSet> >
00231 InstEnvironment::doGetBinding
00232 (const std::string& nm, shared_ptr<InstEnvironment>) const;
00233
00234 template void
00235 Environment<AST>::addBinding(const std::string& nm, shared_ptr<AST> val,
00236 bool rebind);
00237 template void
00238 Environment<TypeScheme>::addBinding(const std::string& nm,
00239 shared_ptr<TypeScheme> val,
00240 bool rebind);
00241 template void
00242 InstEnvironment::addBinding
00243 (const std::string& nm, shared_ptr<InstanceSet> val, bool rebind);
00244
00245
00246 template void
00247 Environment<AST>::removeBinding(const std::string& nm);
00248 template void
00249 Environment<TypeScheme>::removeBinding(const std::string& nm);
00250 template void
00251 InstEnvironment::removeBinding
00252 (const std::string& nm);
00253
00254 template void
00255 Environment<AST>::updateKey(const std::string& from,
00256 const std::string& to);
00257 template void
00258 Environment<TypeScheme>::updateKey(const std::string& from,
00259 const std::string& to);
00260 template void
00261 InstEnvironment::updateKey
00262 (const std::string& from, const std::string& to);
00263
00264
00265 template shared_ptr<Environment<AST> >
00266 Environment<AST>::newScope();
00267 template shared_ptr<Environment<TypeScheme> >
00268 Environment<TypeScheme>::newScope();
00269 template shared_ptr<InstEnvironment >
00270 InstEnvironment::newScope();
00271
00272
00273 template shared_ptr<Environment<AST> >
00274 Environment<AST>::newDefScope();
00275 template shared_ptr<Environment<TypeScheme> >
00276 Environment<TypeScheme>::newDefScope();
00277 template shared_ptr<InstEnvironment >
00278 InstEnvironment::newDefScope();
00279
00280 template bool
00281 Environment<AST>::isAncestor(shared_ptr<Environment<AST> > env);
00282 template bool
00283 Environment<TypeScheme>::isAncestor(shared_ptr<Environment<TypeScheme> > env);
00284 template bool
00285 InstEnvironment::isAncestor
00286 (shared_ptr<InstEnvironment > env);
00287
00288 template void
00289 Environment<AST>::mergeBindingsFrom
00290 (shared_ptr<Environment<AST> > from,
00291 bool complete);
00292 template void
00293 Environment<TypeScheme>::mergeBindingsFrom
00294 (shared_ptr<Environment<TypeScheme> > from,
00295 bool complete);
00296 template void
00297 InstEnvironment::mergeBindingsFrom
00298 (shared_ptr<InstEnvironment > from,
00299 bool complete);
00300
00301 template std::string
00302 Environment<AST>::asString() const;
00303 template std::string
00304 Environment<TypeScheme>::asString() const;
00305 template std::string
00306 InstEnvironment::asString() const;
00307
00308 template
00309 Environment<AST>::~Environment();
00310 template
00311 Environment<TypeScheme>::~Environment();
00312 template
00313 InstEnvironment::~Environment();
00314