TypeChoice.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright (C) 2006, Johns Hopkins University.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or
00007  * without modification, are permitted provided that the following
00008  * conditions are met:
00009  *
00010  *   - Redistributions of source code must contain the above 
00011  *     copyright notice, this list of conditions, and the following
00012  *     disclaimer. 
00013  *
00014  *   - Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions, and the following
00016  *     disclaimer in the documentation and/or other materials 
00017  *     provided with the distribution.
00018  *
00019  *   - Neither the names of the copyright holders nor the names of any
00020  *     of any contributors may be used to endorse or promote products
00021  *     derived from this software without specific prior written
00022  *     permission. 
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  **************************************************************************/
00037 
00038 #include "AST.hxx"
00039 #include "Options.hxx"
00040 #include "UocInfo.hxx"
00041 #include "Type.hxx"
00042 #include "TypeScheme.hxx"
00043 #include "TypeMut.hxx"
00044 #include "Typeclass.hxx"
00045 #include "Trail.hxx"
00046 #include "inter-pass.hxx"
00047 #include "TypeInferCommon.hxx"
00048 
00049 //#define VERBOSE
00050 
00051 
00052 bool
00053 typeInfer(std::ostream& errStream, GCPtr<AST> ast, 
00054           GCPtr<Environment<TypeScheme> > gamma,
00055           GCPtr<Environment< CVector<GCPtr<Instance> > > > instEnv,
00056           GCPtr< CVector<GCPtr<Type> > >impTypes,
00057           bool isVP, 
00058           GCPtr<TCConstraints> tcc,
00059           unsigned long uflags,
00060           GCPtr<Trail> trail,
00061           int mode,
00062           unsigned flags);
00063 
00064 bool
00065 typeEqInfer(std::ostream& errStream, GCPtr<AST> ast, 
00066             GCPtr<Environment<TypeScheme> > gamma,
00067             GCPtr<Environment< CVector<GCPtr<Instance> > > > instEnv,
00068             GCPtr< CVector<GCPtr<Type> > >impTypes,
00069             bool isVP, 
00070             GCPtr<TCConstraints> tcc,
00071             unsigned long uflags,
00072             GCPtr<Trail> trail,
00073             int mode,
00074             unsigned flags);
00075 
00076 std::string
00077 ctypeAsString(GCPtr<Type> t, GCPtr<Constraints> cset,
00078               bool showAllCts=false);
00079   
00080 /**************************************************************/
00081 /*              Interface to the outside world                */
00082 /**************************************************************/
00083 
00084 bool
00085 UocInfo::fe_typeCheck(std::ostream& errStream,
00086                       bool init, unsigned long flags)
00087 {
00088   // Careful: the input flags are interface flags `uflags,'
00089   // and not the internal flags `flags.' 
00090 
00091 #ifdef VERBOSE  
00092     errStream << "Now Processing " << ifName;
00093     errStream << " ast = " << ast->astTypeName();
00094     errStream << std::endl;
00095 #endif
00096   
00097   GCPtr<CVector<GCPtr<Type> > > impTypes = new CVector<GCPtr<Type> >;
00098   GCPtr<Trail> trail = new Trail;
00099   bool errFree = true;
00100 
00101   // FIX: TEMPORARY
00102   if(Options::inferenceAlgorithm == inf_eq)
00103     flags |= TYP_NO_PRELUDE;
00104 
00105   if(init) {
00106     
00107     if(flags & INF_REINIT) {
00108       assert(gamma);      
00109       assert(gamma->parent);
00110       gamma = gamma->parent->newDefScope();
00111 
00112       assert(instEnv);      
00113       assert(instEnv->parent);
00114       instEnv = instEnv->parent->newDefScope();      
00115     }
00116     else {
00117       gamma = new Environment<TypeScheme>(this->uocName);
00118       instEnv = new Environment< CVector<GCPtr<Instance> > >(this->uocName);
00119     }
00120     if((flags & TYP_NO_PRELUDE) == 0)
00121       CHKERR(errFree, initGamma(std::cerr, gamma, instEnv, ast, flags));
00122     
00123     if(!errFree)
00124       return false;
00125   }
00126 
00127   switch(Options::inferenceAlgorithm) {
00128   case inf_hm:
00129     {
00130       CHKERR(errFree, typeInfer(errStream, ast, gamma, instEnv, 
00131                                 impTypes, false, 
00132                                 new TCConstraints, flags, trail, 
00133                                 USE_MODE, TI_NONE));
00134       break;
00135     }
00136   case inf_eq: 
00137     {
00138       CHKERR(errFree, typeEqInfer(errStream, ast, gamma, instEnv, 
00139                                   impTypes, false, 
00140                                   new TCConstraints, flags, trail, 
00141                                   USE_MODE, TI_NONE));
00142       
00143       
00144       errStream << "- - - - - - - - - - - - - - - - - - - - - - - " << endl;
00145       GCPtr<AST> mod = ast->child(0);
00146       for(size_t i=0; i < mod->children->size(); i++) {
00147         GCPtr<AST> ast = mod->child(i);
00148         errStream << ast->atKwd() << std::endl;
00149         if(ast->astType == at_define) {
00150           GCPtr<AST> id = ast->child(0)->child(0);
00151           errStream << id->asString() << ": "   
00152                     << ctypeAsString(id->scheme->tau, id->scheme->tcc)
00153                     << std::endl;
00154         }
00155       }
00156       break;
00157     }
00158   }
00159   
00160   CHKERR(errFree, checkImpreciseTypes(errStream, gamma, impTypes));
00161   
00162 #ifdef VERBOSE  
00163   errStream << "________________________________________" << std::endl;
00164 #endif
00165 
00166   return errFree;
00167 }
00168 

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