Environment.hxx

Go to the documentation of this file.
00001 #ifndef ENVIRONMENT_HXX
00002 #define ENVIRONMENT_HXX
00003 
00004 /**************************************************************************
00005  *
00006  * Copyright (C) 2008, Johns Hopkins University.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or
00010  * without modification, are permitted provided that the following
00011  * conditions are met:
00012  *
00013  *   - Redistributions of source code must contain the above
00014  *     copyright notice, this list of conditions, and the following
00015  *     disclaimer.
00016  *
00017  *   - Redistributions in binary form must reproduce the above
00018  *     copyright notice, this list of conditions, and the following
00019  *     disclaimer in the documentation and/or other materials
00020  *     provided with the distribution.
00021  *
00022  *   - Neither the names of the copyright holders nor the names of any
00023  *     of any contributors may be used to endorse or promote products
00024  *     derived from this software without specific prior written
00025  *     permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00028  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00029  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00030  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00031  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00032  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00033  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00034  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00035  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00036  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00037  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  **************************************************************************/
00040 
00041 #include <iostream>
00042 #include <map>
00043 #include <string>
00044 #include <set>
00045 
00046 #include "shared_ptr.hxx"
00047 
00048 // Type of (sub) environment, if any.
00049 // Universal, In module scope, or in record scope
00050 //enum envType {universal, mod, rec, none};
00051 //enum BindType {bind_type, bind_value};
00052 
00053 
00054 #define BF_PRIVATE   0x1u  /* Binding is a private binding */
00055 #define BF_COMPLETE  0x2u  /* Binding def is completed */
00056 #define BF_REBIND    0x4u  /* On merge, this binding should replace
00057                               any existing binding. */
00058 #define BF_NO_MERGE  0x8u  /* This flag should not survive a merger */
00059 
00060 /* The following two flags should only be found in the private per-UoC
00061    copy of the interface environment that is made in importIfBinding()
00062    in Symtab.cxx */
00063 #define BF_PROVIDING 0x10u /* Interface binding that we are
00064                               providing. */
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; // in the chain of environments
00091   boost::shared_ptr<Environment<T> > defEnv; // definition level env
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   // Updates the most-current binding.
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   // Is env my ancestor?
00195   bool isAncestor(boost::shared_ptr<Environment<T> > env);
00196 
00197   std::string asString() const;
00198 };
00199 
00200 // A couple of kinds of environments that we will be defining
00201 // elsewhere.
00202 
00203 // In abstract, an InstEnvironment is a set. In future, we may use
00204 // lexical resolution for instances, in which case we will either need
00205 // to change this or we will need to make use of lexically nested
00206 // instance environments.
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 /* ENVIRONMENT_HXX */

Generated on Fri Feb 10 07:59:20 2012 for BitC Compiler by  doxygen 1.4.7