TypeScheme.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright (C) 2008, 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 <assert.h>
00039 #include <stdint.h>
00040 #include <stdlib.h>
00041 #include <dirent.h>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <string>
00045 #include <sstream>
00046 
00047 #include <libsherpa/UExcept.hxx>
00048 
00049 #include "Options.hxx"
00050 #include "UocInfo.hxx"
00051 #include "AST.hxx"
00052 #include "Type.hxx"
00053 #include "TypeInfer.hxx"
00054 #include "TypeScheme.hxx"
00055 #include "TypeMut.hxx"
00056 #include "Typeclass.hxx"
00057 #include "inter-pass.hxx"
00058 #include "Unify.hxx"
00059 
00060 using namespace boost;
00061 using namespace sherpa;
00062 using namespace std;
00063 
00064 TypeScheme::TypeScheme(shared_ptr<Type> _tau, shared_ptr<AST> _ast, shared_ptr<TCConstraints> _tcc)
00065 {
00066   tau = _tau;
00067   ast = _ast;
00068   tcc = _tcc;
00069 }
00070 
00071 shared_ptr<Type> 
00072 TypeScheme::type_instance()
00073 {
00074   normalize();
00075 
00076   //std::cout << "Instantiating " << this->asString();
00077 
00078   vector<shared_ptr<Type> > cnftvs;
00079   vector<shared_ptr<Type> > cftvs;
00080   
00081   for (TypeSet::iterator itr_i = ftvs.begin(); 
00082       itr_i != ftvs.end(); ++itr_i) {
00083     cftvs.push_back(*itr_i);
00084     cnftvs.push_back(newTvar());
00085   }
00086   
00087   shared_ptr<Type> t = tau->TypeSpecialize(cftvs, cnftvs); 
00088   //std::cout << " to " << t->asString() << std::endl;
00089 
00090   return t;
00091 }
00092 
00093 shared_ptr<TypeScheme> 
00094 TypeScheme::ts_instance()
00095 {
00096   normalize();
00097 
00098   shared_ptr<TypeScheme> ts = TypeScheme::make(tau, ast);
00099   ts->tau = GC_NULL;
00100 
00101   vector<shared_ptr<Type> > cnftvs;
00102   vector<shared_ptr<Type> > cftvs;
00103   
00104   for (TypeSet::iterator itr_i = ftvs.begin(); 
00105       itr_i != ftvs.end(); ++itr_i) {
00106     shared_ptr<Type> tv = newTvar();
00107     ts->ftvs.insert(tv);
00108     cftvs.push_back(*itr_i);
00109     cnftvs.push_back(tv);
00110   }
00111   
00112   ts->tau = tau->TypeSpecializeReal(cftvs, cnftvs);  
00113   
00114   if (tcc) {
00115     shared_ptr<TCConstraints> _tcc = TCConstraints::make();
00116     addConstraints(_tcc);
00117     
00118     ts->tcc = TCConstraints::make();
00119     for (TypeSet::iterator itr = _tcc->begin();
00120         itr != _tcc->end(); ++itr) {
00121       shared_ptr<Typeclass> pred;
00122       
00123       pred = (*itr)->TypeSpecializeReal(cftvs, cnftvs);
00124       ts->tcc->addPred(pred);
00125     }
00126   }
00127   
00128   tau->clear_sp();
00129   
00130   if (tcc)
00131     for (TypeSet::iterator itr = tcc->begin();
00132         itr != tcc->end(); ++itr)
00133       (*itr)->clear_sp();
00134   
00135   return ts;
00136 }
00137 
00138 void
00139 TypeScheme::addConstraints(shared_ptr<TCConstraints> _tcc) const
00140 {
00141   if (!tcc)
00142     return;
00143   
00144   TypeSet allFtvs;
00145   tau->collectAllftvs(allFtvs);
00146   
00147   for (TypeSet::iterator itr = tcc->begin();
00148       itr != tcc->end(); ++itr) {
00149     for (TypeSet::iterator itr_j = allFtvs.begin();
00150         itr_j != allFtvs.end(); ++itr_j)
00151       if ((*itr)->boundInType(*itr_j)) {
00152         _tcc->addPred(*itr);
00153         break;
00154       }
00155   }
00156   
00157   //if (tcc->pred.size()) {
00158   // std::cout << tau->ast->loc << "AddConstraints("
00159   //              << tau->asString() << ", ";    
00160   // for (size_t i = 0; i < tcc->pred.size(); i++)
00161   //   std::cout << tcc->pred[i]->asString() << ", ";
00162   // std::cout << ") = ";
00163   // for (size_t i = 0; i < _tcc->pred.size(); i++)
00164   //   std::cout << _tcc->pred[i]->asString() << ", ";       
00165   // std::cout << "."
00166   //              << std::endl;
00167   //}
00168 }
00169 
00170 bool
00171 TypeScheme::normalize() 
00172 {
00173   bool changed = false;
00174 
00175   DEBUG(TS_NORM)
00176     std::cerr << "Considering: "
00177               << asString(Options::debugTvP, false)
00178               << std::endl;
00179   
00180   
00181   TypeSet newTvs;
00182   for (TypeSet::iterator itr_c = ftvs.begin();
00183       itr_c != ftvs.end(); ++itr_c) {
00184     shared_ptr<Type> ftv = (*itr_c)->getType();
00185     
00186     if (ftv->typeTag == ty_tvar)
00187       newTvs.insert(ftv);
00188     else
00189       changed = true;
00190   }
00191   ftvs = newTvs;
00192   
00193   if (tcc) {
00194     ConstraintSet allPreds = tcc->pred;
00195     tcc->pred.clear();
00196     
00197     for (TypeSet::iterator itr = allPreds.begin();
00198         itr != allPreds.end(); ++itr) {
00199       shared_ptr<Constraint> ct = (*itr)->getType();
00200       if (!ct->isPcst()) {
00201         tcc->addPred(ct);
00202         continue;
00203       }
00204       
00205       shared_ptr<Type> k = ct->CompType(0)->getType();
00206       shared_ptr<Type> tg = ct->CompType(1)->getType();
00207 
00208       /* If k = M, the solver must have handled this case
00209          and unified tg = ti
00210                        _
00211          If k = P and |_|(tg), then the solver must have handled this
00212          case and unified tg = I(tg) and ti = I(ti).
00213          Actually, we can check Immut(tg) here;
00214 
00215 
00216          In either case, drop this predicate. 
00217          Otherwise, add it to newPred.                  */
00218       
00219       if ((k == Type::Kmono) ||
00220          ((k == Type::Kpoly) && tg->isConcretizable())) {
00221         changed = true;
00222       }
00223       else {
00224         tcc->addPred(ct);
00225       }
00226     }
00227   }
00228   
00229   DEBUG(TS_NORM)
00230     if (changed)
00231       std::cerr << "\t\tNormalized to "
00232                 << asString(Options::debugTvP, false)
00233                 << std::endl;
00234   
00235   return changed;
00236 }

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