BeginSimp.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 /* In order to successfully polyinstantiate instances that contain
00039    immediate lambdas, we need to hoist them and give them proper
00040    names so that the polyinstantiator has something to mangle. */
00041 
00042 #include <assert.h>
00043 #include <stdint.h>
00044 #include <stdlib.h>
00045 #include <dirent.h>
00046 #include <fstream>
00047 #include <iostream>
00048 #include <sstream>
00049 #include <string>
00050 
00051 #include <libsherpa/UExcept.hxx>
00052 
00053 #include "AST.hxx"
00054 #include "inter-pass.hxx"
00055 
00056 using namespace boost;
00057 using namespace sherpa;
00058 
00082 static shared_ptr<AST>
00083 beginSimp(shared_ptr<AST> ast, std::ostream& errStream, bool &errFree)
00084 {
00085   for (size_t c = 0; c < ast->children.size(); c++)
00086     ast->child(c) = beginSimp(ast->child(c), errStream, errFree);
00087 
00088   if (ast->astType == at_begin) {
00089     // Note that the execution of this loop can have the effect of
00090     // shortening ast->children.size()!
00091     for (size_t c = 0; c < ast->children.size(); c++) {
00092       if (ast->child(c)->astType == at_define ||
00093           ast->child(c)->astType == at_recdef) {
00094         bool rec = (ast->child(c)->astType == at_recdef);
00095         shared_ptr<AST> def = ast->child(c);
00096 
00097         // at_usesel is not allowed to appear in a defining occurrence
00098         // of a local at_define or at_recdef.
00099         //
00100          // It might be better to catch this in the parser.
00101         if (def->child(0)->child(0)->astType == at_usesel) {
00102           errStream << def->loc << ": "
00103                     << "Hygienically aliased names cannot be defined locally."
00104                     << std::endl;
00105           errFree = false;
00106         }
00107 
00108         shared_ptr<AST> letBinding =
00109           AST::make(at_letbinding,
00110                   def->loc, def->child(0), def->child(1));
00111         if (rec)
00112           letBinding->flags |= LB_REC_BIND;
00113         
00114         // The definition is not a global
00115         def->child(0)->child(0)->flags &= ~ID_IS_GLOBAL;
00116         shared_ptr<AST> body = AST::make(at_begin, ast->child(c)->loc);
00117         
00118         for (size_t bc = c+1; bc < ast->children.size(); bc++)
00119           body->addChild(ast->child(bc));
00120         
00121         if (body->children.size() == 0) {
00122           errStream << def->loc << ": "
00123                     << "definition not permitted as the "
00124                     << "last expression in a sequece."
00125                     << std::endl;
00126           errFree = false;
00127         }
00128         
00129         // Trim the remaining children of this begin:
00130         // while (ast->children.size() > c+1)
00131         //   ast->children->Remove(ast->children.size()-1);
00132         std::vector<shared_ptr<AST> > newChildren;
00133         for (size_t i=0; i <= c; i++)
00134           newChildren.push_back(ast->child(i));
00135         ast->children = newChildren;
00136         
00137         // Insert the new letrec:
00138         shared_ptr<AST> theLetRec =
00139           AST::make((rec ? at_letrec : at_let), def->loc,
00140                   AST::make(at_letbindings, def->loc, letBinding),
00141                   body, def->child(2));
00142         ast->child(c) = beginSimp(theLetRec, errStream, errFree);
00143 
00144         // Stop processing this begin:
00145         break;
00146       }
00147     }
00148   }
00149 
00150   if (ast->astType == at_begin && ast->children.size() == 1)
00151     return ast->child(0);
00152 
00153   return ast;
00154 }
00155 
00156 bool
00157 UocInfo::fe_beginSimp(std::ostream& errStream,
00158                       bool init, unsigned long flags)
00159 {
00160   DEBUG(BEG_SIMP) if (isSourceUoc())
00161     PrettyPrint(errStream);
00162 
00163   DEBUG(BEG_SIMP) std::cerr << "fe_beginSimp" << std::endl;
00164   bool errFree = true;
00165   uocAst = beginSimp(uocAst, errStream, errFree);
00166 
00167   DEBUG(BEG_SIMP) if (isSourceUoc())
00168     PrettyPrint(errStream);
00169 
00170   return true;
00171 }

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