inter-pass.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 <sstream>
00045 #include <string>
00046 #include <map>
00047 
00048 #include <libsherpa/UExcept.hxx>
00049 
00050 #include "Options.hxx"
00051 #include "UocInfo.hxx"
00052 #include "AST.hxx"
00053 #include "Type.hxx"
00054 #include "TypeInfer.hxx"
00055 #include "inter-pass.hxx"
00056 
00057 using namespace std;
00058 using namespace boost;
00059 using namespace sherpa;
00060 
00061 bool
00062 UocInfo::RandT(std::ostream& errStream,
00063                bool init,
00064                ResolverFlags rflags,
00065                TI_Flags tflags,
00066                std::string mesg)
00067 {
00068   bool errFree = true;
00069 
00070   CHKERR(errFree, Resolve(errStream, init, rflags, mesg));
00071 
00072   if (errFree)
00073     CHKERR(errFree, TypeCheck(errStream, init, tflags, mesg));
00074 
00075   if (!errFree)
00076     errStream << "WHILE R&Ting:" << std::endl
00077               << uocAst->asString() << std::endl;
00078 
00079   return errFree;
00080 }
00081 
00082 
00083 void
00084 UocInfo::wrapEnvs()
00085 {
00086   env = env->newDefScope();
00087   gamma = gamma->newDefScope();
00088   instEnv = instEnv->newDefScope();
00089 }
00090 
00091 void
00092 UocInfo::unwrapEnvs()
00093 {
00094   env = env->defEnv;
00095   gamma = gamma->defEnv;
00096   instEnv = instEnv->defEnv;
00097 }
00098 
00099 // The following is used to R&T any sub-expression
00100 
00101 bool
00102 UocInfo::RandTexpr(std::ostream& errStream,
00103                    shared_ptr<AST> expr,
00104                    ResolverFlags rflags,
00105                    TI_Flags tflags,
00106                    std::string mesg,
00107                    bool keepResults,
00108                    shared_ptr<EnvSet> altEnvSet)
00109 {
00110   bool errFree = true;
00111   shared_ptr<UocInfo> myUoc = UocInfo::make(shared_from_this());
00112   myUoc->uocAst = expr;
00113 
00114   if (altEnvSet) {
00115     myUoc->env = altEnvSet->env;
00116     myUoc->gamma = altEnvSet->gamma;
00117     myUoc->instEnv = altEnvSet->instEnv;
00118   }
00119 
00120   if (!keepResults)
00121     myUoc->wrapEnvs();
00122 
00123   CHKERR(errFree, myUoc->Resolve(errStream, false, rflags, mesg));
00124   if (errFree)
00125     CHKERR(errFree, myUoc->TypeCheck(errStream, false,
00126                                      tflags, mesg)); 
00127   
00128   if (!keepResults)
00129     myUoc->unwrapEnvs();
00130 
00131   if (!errFree)
00132     errStream << "WHILE R&Ting:" << std::endl
00133               << expr->asString() << std::endl;
00134 
00135   return errFree;
00136 }
00137 
00138 
00139 #define MARKDEF(ast, def) do {\
00140     assert(def);              \
00141     ast->defForm = def;              \
00142   } while (0);
00143 //std::cout << "Marked " << ast->asString() << "->defForm = "
00144 //        << def->asString() << std::endl;
00145 
00146 
00151 void
00152 UocInfo::findDefForms(shared_ptr<AST> ast, shared_ptr<AST> local, shared_ptr<AST> top)
00153 {
00154   bool processChildren = true;
00155 
00156   switch(ast->astType) {
00157   case at_let:
00158   case at_letrec:
00159   case at_letStar:
00160     {
00161       MARKDEF(ast, top);
00162       local = ast;
00163       break;
00164     }
00165 
00166   case at_letbindings:
00167     {
00168       assert(local != top);
00169       MARKDEF(ast, local);
00170       local = ast;
00171       break;
00172     }
00173 
00174   case at_letbinding:
00175     {
00176       MARKDEF(ast, local);
00177       shared_ptr<AST> id = ast->child(0)->child(0);
00178       MARKDEF(id, ast);
00179       break;
00180     }
00181 
00182   case at_define:
00183   case at_recdef:
00184     {
00185       shared_ptr<AST> id = ast->child(0)->child(0);
00186       MARKDEF(id, ast);
00187       top = ast;
00188       local = ast;
00189       break;
00190     }
00191 
00192   case at_declstruct:
00193   case at_declunion:
00194   case at_proclaim:
00195   case at_defstruct:
00196   case at_defexception:
00197     {
00198       shared_ptr<AST> id = ast->child(0);
00199       MARKDEF(id, ast);
00200       processChildren = false;
00201       break;
00202     }
00203 
00204   case at_defunion:
00205     {
00206       shared_ptr<AST> id = ast->child(0);
00207       MARKDEF(id, ast);
00208       shared_ptr<AST> ctrs = ast->child(4);
00209       for (size_t i=0; i < ctrs->children.size(); i++) {
00210         shared_ptr<AST> ctr = ctrs->child(i);
00211         shared_ptr<AST> ctrID = ctr->child(0);
00212         MARKDEF(ctrID, ast);
00213       }
00214       processChildren = false;
00215       break;
00216     }
00217 
00218   case at_deftypeclass:
00219     {
00220       shared_ptr<AST> id = ast->child(0);
00221       MARKDEF(id, ast);
00222         
00223       shared_ptr<AST> methods = ast->child(4);
00224       for (size_t i = 0; i < methods->children.size(); i++) {
00225         shared_ptr<AST> method = methods->child(i);
00226         shared_ptr<AST> mID = method->child(0);
00227         MARKDEF(mID, ast);
00228       }
00229       processChildren = false;
00230       break;
00231     }
00232 
00233   case at_loop:
00234     {
00235       local = ast;
00236       break;
00237     }
00238 
00239   case at_loopbindings:
00240     {
00241       assert(local != top);
00242       MARKDEF(ast, local);
00243       local = ast;
00244       break;
00245     }
00246 
00247   case at_loopbinding:
00248     {
00249       MARKDEF(ast, local);
00250       shared_ptr<AST> id = ast->child(0)->child(0);
00251       MARKDEF(id, ast);
00252       break;
00253     }
00254 
00255   default:
00256     {
00257       break;
00258     }
00259   }
00260 
00261   if (processChildren)
00262     for (size_t c=0; c < ast->children.size(); c++)
00263       findDefForms(ast->child(c), local, top);        
00264 }
00265 
00268 void
00269 UocInfo::findAllDefForms()
00270 {
00271   for (UocMap::iterator itr = UocInfo::srcList.begin();
00272       itr != UocInfo::srcList.end(); ++itr) {
00273     shared_ptr<UocInfo> puoci = itr->second;
00274     puoci->findDefForms(puoci->uocAst);
00275   }
00276 
00277   for (UocMap::iterator itr = UocInfo::ifList.begin();
00278       itr != UocInfo::ifList.end(); ++itr) {
00279     shared_ptr<UocInfo> puoci = itr->second;
00280     puoci->findDefForms(puoci->uocAst);
00281   }
00282 }
00283 
00284 static void
00285 addCandidates(shared_ptr<AST> mod)
00286 {
00287   for (size_t c = 0; c < mod->children.size(); c++) {
00288     shared_ptr<AST> ast = mod->child(c);
00289     shared_ptr<AST> id = ast->getID();
00290     switch(ast->astType) {
00291     case at_proclaim: // proclaims needed to keep externalNames
00292     case at_define:
00293     case at_recdef:
00294     case at_defexception:
00295       if (id->symType->isConcrete())
00296         Options::entryPts.insert(id->fqn.asString());
00297 
00298       break;
00299 
00300     default:
00301         break;
00302     }
00303   }
00304 }
00305 
00306 
00307 void
00308 UocInfo::addAllCandidateEPs()
00309 {
00310   for (UocMap::iterator itr = UocInfo::ifList.begin();
00311       itr != UocInfo::ifList.end(); ++itr) {
00312     shared_ptr<UocInfo> puoci = itr->second;
00313     addCandidates(puoci->uocAst);
00314   }
00315 
00316   for (UocMap::iterator itr = UocInfo::srcList.begin();
00317       itr != UocInfo::srcList.end(); ++itr) {
00318     shared_ptr<UocInfo> puoci = itr->second;
00319     addCandidates(puoci->uocAst);
00320   }
00321 
00322   //for (size_t c=0; c < Options::entryPts.size(); c++)
00323   //  std::cerr << "Entry Point: " << Options::entryPts[c] << std::endl;
00324 }
00325 

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