UocInfo.hxx

Go to the documentation of this file.
00001 #ifndef UOCINFO_HXX
00002 #define UOCINFO_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 <set>
00042 #include <string>
00043 #include <ostream>
00044 
00045 #include <boost/filesystem/path.hpp>
00046 #include <libsherpa/LexLoc.hxx>
00047 
00048 #include "Environment.hxx"
00049 #include "AST.hxx"
00050 #include "TypeScheme.hxx"
00051 #include "Symtab.hxx"
00052 #include "TypeInfer.hxx"
00053 #include "WorkList.hxx"
00054 
00055 enum UocFlagValues {
00056   UOC_NO_FLAGS = 0,
00057 
00061   UOC_IS_PRELUDE   = 0x1u,
00062 
00068   UOC_IS_MUTABLE   = 0x2u,
00069 
00071   UOC_SEEN_BY_INST = 0x4u,
00072 };
00073 
00074 typedef sherpa::EnumSet<UocFlagValues> UocFlags;
00075 
00076 // Passes consist of transforms on the AST. The parse pass is included in 
00077 // the list for debugging annotation purposes, but is handled as a special
00078 // case.
00079 
00080 enum Pass {
00081 #define PASS(nm, short, descrip) pn_##nm,
00082 #include "pass.def"
00083 };
00084 
00085 enum OnePass {
00086 #define ONEPASS(nm, short, descrip) op_##nm,
00087 #include "onepass.def"
00088 };
00089 
00090 class UocInfo;
00091 
00092 struct PassInfo {
00093   const char *name;
00094   bool (UocInfo::* fn)(std::ostream& errStream, 
00095                        bool init, unsigned long flags);
00096   const char *descrip;
00097   bool printAfter;                // for debugging
00098   bool typesAfter;                // for debugging
00099   bool stopAfter;                // for debugging
00100 };
00101 
00102 struct OnePassInfo {
00103   const char *name;
00104   bool (UocInfo::* fn)(std::ostream& errStream, 
00105                        bool init, unsigned long flags);
00106   const char *descrip;
00107   bool printAfter;                // for debugging
00108   bool typesAfter;                // for debugging
00109   bool stopAfter;                // for debugging
00110 };
00111 
00112 
00113 class UocInfo;
00114 typedef std::map<std::string, boost::shared_ptr<UocInfo> > UocMap;
00115 
00116 // Unit Of Compilation Info. One of these is constructed for each
00117 // unit of compilation (source or interface). In the source case, the
00118 // /name/ field will be the file name. In the interface case, the
00119 // /name/ will be the "ifname" reported in the import statement.
00120 class UocInfo : public boost::enable_shared_from_this<UocInfo> {
00121   static boost::filesystem::path resolveInterfacePath(std::string ifName);
00122 
00123 public:
00124   std::string uocName;                // either ifName or simulated
00125   std::string origin;                // typically the file name
00126   UocFlags flags;
00127   
00128   Pass lastCompletedPass;
00129 
00130   bool fromCommandLine;
00131   boost::shared_ptr<AST> uocAst;
00132   boost::shared_ptr<ASTEnvironment > env;
00133   boost::shared_ptr<TSEnvironment > gamma;
00134   boost::shared_ptr<InstEnvironment > instEnv;
00135 
00136   inline bool isSourceUoc() {
00137     return (uocAst->astType == at_module);
00138   }
00139 
00140   inline bool isInterfaceUoc() {
00141     return (uocAst->astType == at_interface);
00142   }
00143 
00144   
00145   UocInfo(const std::string& _uocName, const std::string& _origin, 
00146           boost::shared_ptr<AST> _uocAst);
00147   UocInfo(boost::shared_ptr<UocInfo> uoc);
00148 
00149   static inline boost::shared_ptr<UocInfo>
00150   make(const std::string& _uocName, const std::string& _origin, 
00151           boost::shared_ptr<AST> _uocAst) {
00152     UocInfo *tmp = new UocInfo(_uocName, _origin, _uocAst);
00153     return boost::shared_ptr<UocInfo>(tmp);
00154   }
00155 
00156   static inline boost::shared_ptr<UocInfo>
00157   make(boost::shared_ptr<UocInfo> uoc) {
00158     UocInfo *tmp = new UocInfo(uoc);
00159     return boost::shared_ptr<UocInfo>(tmp);
00160   }
00161 
00162   /* Create a fresh, empty UOC that is set up to become the unified
00163    * output UoC module, initializing all environments by hand.
00164    */
00165   static boost::shared_ptr<UocInfo> CreateUnifiedUoC();
00166 
00167   static std::vector<boost::filesystem::path> searchPath;
00168 
00169   // FIX -- All of these should be locals
00170 
00171   // The presence of a UocInfo record in the ifList indicates that
00172   // parsing of an interface has at least started. If the /ast/ pointer
00173   // is non-null, the parse has been completed.
00174   static UocMap ifList;
00175   static UocMap srcList;
00176   //  static boost::shared_ptr<UocInfo> linkedUoc;  // grand Uoc after linkage
00177 
00178   static boost::shared_ptr<UocInfo> 
00179   findInterface(const std::string& ifName);
00180 
00181   static boost::shared_ptr<UocInfo> 
00182   importInterface(std::ostream&, const sherpa::LexLoc& loc, 
00183                   const std::string& ifName);
00184 
00185   // This is only used within the parser, and perhaps should not be
00186   // part of UocInfo.
00187   static std::string 
00188   UocNameFromSrcName(const std::string& srcFileName, unsigned ndx);
00189 
00190   // Parse a file, admitting source and/or interface units of
00191   // compilation into the ifList or the srcList as a side effect.
00192   static bool 
00193   CompileFromFile(const boost::filesystem::path& src, bool fromCmdLine);
00194 
00195   // Individual passes:
00196 #define PASS(nm,short,descrip)                                  \
00197   bool fe_##nm(std::ostream& errStream,                          \
00198                bool init=true, unsigned long flags=0);
00199 #include "pass.def"
00200 
00201 #define ONEPASS(nm,short,descrip)                                \
00202   bool be_##nm(std::ostream& errStream,                                \
00203                bool init=true, unsigned long flags=0);
00204 #include "onepass.def"  
00205 
00206   static PassInfo passInfo[];  
00207   static OnePassInfo onePassInfo[];
00208 
00209   static bool mainIsDefined;
00210   void Compile();
00211   void DoBackend();
00212 
00213   void PrettyPrint(std::ostream& out, bool decorate=false);
00214   void ShowTypes(std::ostream& out);
00215  
00216 
00218   //
00219   // FOLLOWING ARE IN inter-pass.cxx
00220   //
00222   void findDefForms(boost::shared_ptr<AST> start, 
00223                     boost::shared_ptr<AST> local=boost::GC_NULL, 
00224                     boost::shared_ptr<AST> top=boost::GC_NULL);
00225   static void findAllDefForms();
00226   
00227   // Add all candidate Entry-points to the entry-point vectror
00228   static void addAllCandidateEPs();
00229   
00230   /* Build/Remove a newDefScope wrapper that can be used to
00231      temporarily add definitions into them and later commit/abort */
00232   void wrapEnvs();
00233   void unwrapEnvs();
00234   
00235 // Post inference Flags
00236 #define PI_SYM_FLAGS (RSLV_NO_RESOLVE_DECL)
00237 #define PI_TYP_FLAGS (TI_ALL_INSTS_OK)
00238 
00239 // RandTflags used by onePass definitions
00240 #define OP_SYM_FLAGS (PI_SYM_FLAGS | RSLV_SYM_NO_PRELUDE)
00241 #define OP_TYP_FLAGS (PI_TYP_FLAGS | TI_NO_PRELUDE)
00242 
00243 // RandT flags used by passes past polyinstantiation. 
00244 #define POLY_SYM_FLAGS (OP_SYM_FLAGS)
00245 #define POLY_TYP_FLAGS (OP_TYP_FLAGS | TI_NO_MORE_TC |                \
00246                         TI_DEF_DECL_NO_MATCH | TI_USING_FQNS)
00247   
00248 // RandT flags used by passes Refization pass of Closure-conversion.
00249 #define REF_SYM_FLAGS (POLY_SYM_FLAGS | RSLV_INCOMPLETE_NO_CHK)
00250 #define REF_TYP_FLAGS (POLY_TYP_FLAGS)
00251 
00252 // RandT flags used by passes past Closure-conversion. 
00253 #define CL_SYM_FLAGS (REF_SYM_FLAGS)
00254 #define CL_TYP_FLAGS (REF_TYP_FLAGS)
00255 
00256 
00257   bool DoResolve(std::ostream& errStream, bool init, 
00258                ResolverFlags rflags);
00259 
00260   bool Resolve(std::ostream& errStream, bool init, 
00261                ResolverFlags rflags, std::string pre);
00262 
00263   bool 
00264   DoTypeCheck(std::ostream& errStream, bool init, 
00265               TI_Flags ti_flags);
00266   
00267   bool 
00268   TypeCheck(std::ostream& errStream, bool init, 
00269             TI_Flags ti_flags, std::string pre);
00270   
00271   bool RandT(std::ostream& errStream,
00272              bool init=false, 
00273              ResolverFlags rflags= RSLV_NO_FLAGS,
00274              TI_Flags ti_flags=TI_NO_FLAGS,
00275              std::string pre = "Internal Compiler error :");
00276 
00277   bool RandTexpr(std::ostream& errStream,
00278                  boost::shared_ptr<AST> ast,
00279                  ResolverFlags rflags= RSLV_NO_FLAGS,
00280                  TI_Flags ti_flags=TI_NO_FLAGS,
00281                  std::string pre = "Internal Compiler error :",
00282                  bool keepResults = true,
00283                  boost::shared_ptr<EnvSet> altEnvSet=boost::GC_NULL);
00284 
00286   //
00287   // New instantiation logic:
00288   //
00290   static boost::shared_ptr<AST> lookupByFqn(const FQName& fqn, 
00291                           boost::shared_ptr<UocInfo> &targetUoc);
00292   
00293 
00294 private:
00295   void addTopLevelForm(boost::shared_ptr<AST> ast); // Add a new Top-level form
00296 
00297   bool instantiateFQN(std::ostream &errStream, 
00298                       const FQName& fqn);
00299 
00300   // The main AST specializer/ instantiator
00301   boost::shared_ptr<AST> doInstantiate(std::ostream &errStream, 
00302                      boost::shared_ptr<AST> defAST, 
00303                      boost::shared_ptr<Type> typ,
00304                      bool &errFree,
00305                      WorkList<std::string>& worklist);
00306 
00307   boost::shared_ptr<AST> recInstantiate(std::ostream &errStream, 
00308                       boost::shared_ptr<AST> ast,
00309                       bool &errFree,
00310                       WorkList<std::string>& worklist); // Recursively walk the
00311                      // specialized AST, and specialize the body.
00312 
00313 public:
00315   bool instantiate(std::ostream &errStream, 
00316                    const FQName& fqn);
00317   
00319   bool instantiateBatch(std::ostream &errStream, 
00320                         std::set<FQName>& epName);
00321 
00322 };
00323 
00324 #endif /* UOCINFO_HXX */

Generated on Thu May 17 23:59:16 2012 for BitC Compiler by  doxygen 1.4.7