Type-mangle.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 "UocInfo.hxx"
00050 #include "AST.hxx"
00051 #include "Type.hxx"
00052 #include "TypeInfer.hxx"
00053 #include "TypeScheme.hxx"
00054 #include "TypeMut.hxx"
00055 #include "Typeclass.hxx"
00056 #include "inter-pass.hxx"
00057 #include "Unify.hxx"
00058 
00059 using namespace boost;
00060 using namespace sherpa;
00061 using namespace std;
00062 
00063 /* arg1: Ignore all mutability in name 
00064    arg2: Ignore TopLevel mutability and top-level mutability at 
00065          the (first) toplevel fn-arg-ret positions in name */
00066 string
00067 Type::mangledString(bool igMut, bool igTlMut, bool maxArgMut)
00068 {
00069   // Reserved start characters: (Do not use)
00070   // "__" "VS"
00071 
00072   stringstream ss;
00073   
00074   if (getType() != shared_from_this())
00075     return getType()->mangledString(igMut, igTlMut, maxArgMut);
00076 
00077   if (mark & MARK_MANGLED_STRING) {
00078     // Encountered Infinite Type
00079     assert(false);
00080   }
00081 
00082   mark |= MARK_MANGLED_STRING;
00083 
00084   switch(typeTag) {
00085   case ty_tvar:
00086     {
00087       stringstream avss;
00088       avss << "'a" << uniqueID;
00089 
00090       ss << "_" << avss.str().size() << avss.str();
00091       break;
00092     }
00093     
00094   case ty_dummy:
00095     {
00096       ss << "D";
00097       break;
00098     }
00099 
00100   case ty_unit:
00101     ss << "_4unit";
00102     break;
00103 
00104   case ty_bool:
00105   case ty_char:
00106   case ty_string:
00107   case ty_int8:
00108   case ty_int16:
00109   case ty_int32:
00110   case ty_int64:
00111   case ty_uint8:
00112   case ty_uint16:
00113   case ty_uint32:
00114   case ty_uint64:
00115   case ty_word:
00116   case ty_float:
00117   case ty_double:
00118   case ty_quad:
00119     {
00120       const char *tn = typeTagName();
00121       ss << "_" << strlen(tn) << tn;
00122       break;
00123     }
00124 
00125 #ifdef KEEP_BF
00126   case  ty_bitfield:
00127     ss << "BF" << Isize << CompType(0)->mangledString(igMut, false);
00128     break;
00129 #endif
00130 
00131   case ty_method:
00132     ss << "H" 
00133        << Args()->mangledString(igMut, true,maxArgMut)
00134        << Ret()->mangledString(igMut, true, maxArgMut);
00135     break;
00136 
00137   case ty_fn:
00138     ss << "FN" 
00139        << Args()->mangledString(igMut, true,maxArgMut)
00140        << Ret()->mangledString(igMut, true, maxArgMut);
00141 
00142     break;
00143 
00144   case ty_fnarg:
00145     ss << components.size();
00146     for (size_t i=0; i<components.size(); i++) {
00147       if (CompFlags(i) & COMP_BYREF)
00148         ss << "Y";
00149       
00150       ss << CompType(i)->mangledString(igMut, true, 
00151                                        maxArgMut);
00152     }
00153     break;
00154 
00155   case ty_letGather:
00156     ss << "LG";
00157     for (size_t i=0; i<components.size(); i++) {
00158       ss << CompType(i)->mangledString(igMut, igTlMut, 
00159                                               maxArgMut);
00160     }
00161     
00162     break;
00163 
00164   case ty_structv:
00165   case ty_structr:
00166     {
00167       ss << "S"
00168          << ((typeTag == ty_structv) ? "V" : "R")
00169          << typeArgs.size() 
00170          << "_" << defAst->s.size() << defAst->s;
00171 
00172       for (size_t i=0; i < typeArgs.size(); i++)
00173         ss << TypeArg(i)->mangledString(igMut, false, 
00174                                          maxArgMut);
00175       break;
00176     }
00177 
00178   case ty_typeclass:    
00179     {
00180       ss << "TC"
00181          << "_" << defAst->s.size() << defAst->s;
00182       for (size_t i=0; i < typeArgs.size(); i++)
00183         ss << TypeArg(i)->mangledString(igMut, false, 
00184                                          maxArgMut);
00185       
00186       break;
00187     }
00188 
00189   case ty_uconv:
00190   case ty_uconr:
00191   case ty_uvalv:
00192   case ty_uvalr:
00193   case ty_unionv:
00194   case ty_unionr:
00195     {
00196       ss << "U"
00197          << (isRefType() ? "R" : "V")
00198          << typeArgs.size() 
00199          << "_" << myContainer->s.size() << myContainer->s;
00200 
00201       for (size_t i=0; i < typeArgs.size(); i++)
00202         ss << TypeArg(i)->mangledString(igMut, false, maxArgMut);
00203     }
00204     break;
00205 
00206   case ty_array:
00207     {
00208       ss << "J" << Base()->mangledString(igMut, false, maxArgMut)
00209          << "__" << arrLen->len;
00210       
00211       break;
00212     }
00213     
00214   case ty_vector:
00215     {
00216       ss << "K" << Base()->mangledString(igMut, false, maxArgMut);
00217       break;
00218     }
00219 
00220   case ty_ref:
00221     ss << "R" << Base()->mangledString(igMut, false, maxArgMut);
00222     break;
00223 
00224   case ty_byref:
00225     ss << "Z" << Base()->mangledString(igMut, false, maxArgMut);
00226     break;
00227     
00228   case ty_array_ref:
00229     ss << "W" << Base()->mangledString(igMut, false, maxArgMut);
00230     break;
00231 
00232   case ty_mutable:
00233     if (!igMut && !igTlMut)
00234       ss << "M";
00235     ss << Base()->mangledString(igMut, false, maxArgMut);
00236     break;
00237     
00238     // mangleString is called by routines beyond polyinstantiation,
00239     // at which point, the const-ness of normalized the type 
00240     // does not matter (const is really only necessary to 
00241     // constrain type variables.
00242   case ty_const:
00243     ss << Base()->mangledString(igMut, false, maxArgMut);
00244     break;
00245     
00246   case ty_exn:
00247     {
00248       string s = "exception";
00249       ss << "_" << s.size() << s;
00250       //ss << "X" << defAst->s.size() << defAst->s;
00251       break;
00252     }
00253     
00254   case ty_mbFull:
00255   case ty_mbTop:
00256     {
00257       ss << Core()->mangledString(igMut, false, maxArgMut);
00258       break;
00259     }
00260 
00261   case ty_tyfn:
00262   case ty_pcst:
00263   case ty_kvar:
00264   case ty_kfix:
00265   case ty_field:
00266     {
00267       assert(false);
00268       break;
00269     }
00270   }
00271   
00272   mark &= ~MARK_MANGLED_STRING;
00273   return ss.str();
00274 }

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