Gather.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 <stdint.h>
00039 #include <stdlib.h>
00040 #include <dirent.h>
00041 #include <assert.h>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <string>
00045 #include <sstream>
00046 #include <gmp.h>
00047 #include <errno.h>
00048 
00049 #include "Version.hxx"
00050 #include "UocInfo.hxx"
00051 #include "Options.hxx"
00052 #include "AST.hxx"
00053 #include "Type.hxx"
00054 #include "inter-pass.hxx"
00055 #include "Special.hxx"
00056 
00057 using namespace sherpa;
00058 using namespace std;
00059  
00060 static void
00061 fixWithFqns(AST *ast, string sname)
00062 {
00063   switch(ast->astType) {
00064   case at_start:
00065     {
00066       fixWithFqns(ast->children[0], sname);
00067       break;
00068     }
00069 
00070   case at_module:
00071   case at_interface:
00072     {
00073       size_t c = (ast->astType == at_interface) ? 1 : 0;
00074       for (; c < ast->children.size(); c++) {
00075         fixWithFqns(ast->children[c], sname);
00076       }
00077       break;
00078     }
00079   
00080   case at_field:
00081     {
00082       fixWithFqns(ast->children[1], sname);
00083       break;
00084     }
00085 
00086   case at_declares:
00087     break;
00088 
00089   case at_select:
00090     {
00091       fixWithFqns(ast->children[0], sname);
00092       
00093       if((ast->Flags2 & SEL_FROM_UN_VAL) ||
00094          (ast->Flags2 & SEL_FROM_UN_TYPE)) 
00095         fixWithFqns(ast->children[1], sname);
00096       
00097       break;
00098     }
00099   
00100   case at_use_case:
00101     {
00102       ast->children[0]->Flags |= ID_IS_GLOBAL;
00103       ast->children[0]->fqn = ast->children[1]->fqn;
00104       for (size_t c = 0; c < ast->children.size(); c++)
00105         fixWithFqns(ast->children[c], sname);      
00106       break;
00107     }
00108     
00109   case at_ident:
00110     {      
00111       AST *def = ast;
00112       if(ast->symbolDef != NULL)
00113         def = ast->symbolDef;
00114 
00115       if(def->isGlobal()) {
00116         ast->s = def->fqn.asString(".", sname);
00117       }
00118       break;
00119     }
00120 
00121   default:
00122     {
00123       for (size_t c = 0; c < ast->children.size(); c++) {
00124         fixWithFqns(ast->children[c], sname);
00125       } 
00126       break;
00127     }
00128   }
00129 }
00130 
00131 void
00132 getAllDefs(AST *last, AST *ast)
00133 {
00134   switch(ast->astType) {
00135   case at_start:
00136     {
00137       getAllDefs(last, ast->children[0]);
00138       break;
00139     }
00140   case at_module:
00141   case at_interface:
00142     {
00143       size_t c = (ast->astType == at_interface) ? 1 : 0;
00144       for (; c < ast->children.size(); c++) {
00145         switch(ast->children[c]->astType) {
00146         case at_import:
00147         case at_provide:
00148         case at_use:
00149           break;
00150           
00151           // FIX: Why isn't this just default?
00152         case at_definstance:
00153           last->children.append(ast->children[c]);
00154           break;
00155 
00156         default:
00157           last->children.append(ast->children[c]);
00158           break;
00159         }
00160       }
00161       break;
00162     }
00163   default:
00164     {
00165       assert(false);
00166     }
00167   }
00168 }
00169 
00170 bool
00171 markPreludeDefs(AST *prelude)
00172 {
00173 
00174   AST *iface = prelude->children[0];
00175   assert(iface->astType == at_interface);
00176   for(size_t c=1; c < iface->children.size(); c++) {
00177     AST *ast = iface->children[c]->getID();
00178     if(ast != NULL) 
00179       ast->Flags |= DEF_IN_PRELUDE;
00180   }
00181 
00182   return true;
00183 }
00184 
00185 
00186 bool
00187 FQNize()
00188 {
00189   for(size_t i = 0; i < UocInfo::ifList.size(); i++) {
00190     UocInfo *uoc = UocInfo::ifList[i];
00191     fixWithFqns(uoc->ast, "");
00192   }
00193   
00194   for(size_t i = 0; i < UocInfo::srcList.size(); i++) {
00195     UocInfo *uoc = UocInfo::srcList[i];    
00196     stringstream ss;
00197     ss << "src" << i;
00198     fixWithFqns(uoc->ast, ss.str());    
00199   }
00200   return true;
00201 }
00202 
00203 static bool 
00204 reRandT(std::ostream &errStream, UocInfo *uoc)
00205 {
00206    bool errFree = true;
00207    CHKERR(errFree, uoc->RandT(errStream,  
00208                          true, OP_SYM_FLAGS, OP_TYP_FLAGS, 
00209                          "Gather-stage error(s):\n"));
00210    //if(!errFree)
00211    //  errStream << " while RandTing " << uoc->ast->asString() << std::endl;
00212    
00213   return errFree;
00214 }
00215 
00216 bool
00217 buildLinkedAST(std::ostream &errStream, UocInfo *luoc)
00218 {
00219   bool errFree = true;
00220 
00221   sherpa::LexLoc loc; // internal location
00222   luoc->ast = new AST(at_start, loc);
00223   AST *start = luoc->ast;
00224   AST *verStr = AST::makeStringLit(LToken(loc, BITC_VERSION));
00225   AST *ver = new AST(at_version, loc, verStr);
00226   AST *mod = new AST(at_module, loc);
00227   start->children.append(mod);
00228   start->children.append(ver);
00229   
00230   // First process prelude
00231   for(size_t i = 0; i < UocInfo::ifList.size(); i++) {
00232     UocInfo *uoc = UocInfo::ifList[i];
00233     if(uoc->flags & UOC_IS_PRELUDE) {
00234       //markAlwaysEmit(uoc);
00235       getAllDefs(mod, uoc->ast); 
00236       break;
00237     }
00238   }
00239 
00240   // Next all interfaces
00241   for(size_t i = 0; i < UocInfo::ifList.size(); i++) {
00242     UocInfo *uoc = UocInfo::ifList[i];
00243     if(uoc->flags & UOC_IS_PRELUDE) 
00244       continue;
00245     
00246     getAllDefs(mod, uoc->ast);
00247   }
00248 
00249   // Next all source units.
00250   for(size_t i = 0; i < UocInfo::srcList.size(); i++) {
00251     UocInfo *uoc = UocInfo::srcList[i];    
00252     getAllDefs(mod, uoc->ast);
00253   }
00254 
00255   CHKERR(errFree, reRandT(errStream, luoc));
00256   //BitcP(std::cout, start, false);
00257   //DisplayTypes(std::cout, luoc);
00258   return errFree;
00259 }
00260 
00261 
00262 #if 0
00263 bool 
00264 ensureDefnsFound(std::ostream &errStream, AST *start)
00265 {
00266   bool errFree = true;
00267   AST *mod = start->children[0];
00268   
00269   for(size_t c = 0; c < mod->children.size(); c++) {
00270     AST *ast = mod->children[c];
00271     switch(ast->astType) {
00272     case at_declstruct:
00273     case at_declunion:    
00274     case at_declValue:
00275       {
00276         AST *def = ast->getID();
00277         if(def->defn == NULL && 
00278            ((def->Flags & DEF_IS_EXTERNAL) == 0)) {
00279           // Declaration Found, No definition.
00280           errStream << ast->loc.asString()
00281                     << ": non-external symbol " << def->s 
00282                     << " proclaimed here, but is never defined."
00283                     << endl;
00284           errFree = false;
00285         }
00286         break;
00287       }
00288     
00289     default:
00290       break;    
00291     }
00292   }
00293   return errFree;
00294 }
00295 #endif
00296 
00297 bool
00298 UocInfo::be_gather(std::ostream &errStream, 
00299                    bool init, unsigned long flags)
00300 {
00301   bool errFree = true;
00302   
00303   UocInfo *prelude = NULL;
00304   for(size_t i = 0; i < UocInfo::ifList.size(); i++) {
00305     UocInfo *uoc = UocInfo::ifList[i];
00306     if(uoc->flags & UOC_IS_PRELUDE) {
00307       prelude = uoc;
00308       break;
00309     }
00310   }
00311 
00312   assert(prelude != NULL);
00313   AST *preludeAST = prelude->ast;
00314 
00315   CHKERR(errFree, markPreludeDefs(preludeAST));
00316   CHKERR(errFree, FQNize());
00317   SpecialNames::spNames.fixUpSpNames(prelude);
00318   CHKERR(errFree, buildLinkedAST(errStream, this));
00319   
00320   if(!ast->isValid()) {
00321     errStream << "PANIC: Invalid AST built for LINKED AST"
00322               << "Please report this problem.\n"; 
00323     errStream << ast->asString() << std::endl;
00324     exit(1);
00325   }
00326   markDefForms(ast);
00327   
00328 #if 0
00329   CHKERR(errFree, ensureDefnsFound(errStream, uoc->ast)); 
00330 #endif
00331 
00332 #if 0
00333   if((Options::backEnd->flags & BK_HDR_MODE) == 0) 
00334     CHKERR(errFree, checkForMain(errStream, uoc));
00335 #endif
00336   
00337   return errFree;
00338 }
00339 

Generated on Sat Feb 4 23:59:28 2012 for BitC Compiler by  doxygen 1.4.7