00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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 #include <libsherpa/UExcept.hxx>
00047
00048 #include "Options.hxx"
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 static string
00064 printName(shared_ptr<AST> ast)
00065 {
00066 if (!ast)
00067 return "NULL";
00068
00069 return ast->s;
00070 }
00071
00072 void
00073 Type::asXML(shared_ptr<TvPrinter> tvP, INOstream &out)
00074 {
00075 if (Options::rawTvars)
00076 tvP = GC_NULL;
00077
00078 shared_ptr<Type> t = getType();
00079
00080 if (t->pMark >= 2) {
00081 out << "<infinity/>" << endl;
00082 return;
00083 }
00084 else {
00085 t->pMark++;
00086 }
00087
00088 switch(t->typeTag) {
00089 case ty_unit:
00090 {
00091 out << "<unit/>" << endl;
00092 break;
00093 }
00094
00095 case ty_bool:
00096 case ty_char:
00097 case ty_string:
00098 case ty_word:
00099 case ty_float:
00100 case ty_double:
00101 case ty_quad:
00102 {
00103 out << "<" << t->typeTagName() << "/>" << endl;
00104 break;
00105 }
00106 case ty_int8:
00107 {
00108 out << "<int sz='8'/>" << endl;
00109 break;
00110 }
00111 case ty_int16:
00112 {
00113 out << "<int sz='16'/>" << endl;
00114 break;
00115 }
00116 case ty_int32:
00117 {
00118 out << "<int sz='32'/>" << endl;
00119 break;
00120 }
00121 case ty_int64:
00122 {
00123 out << "<int sz='64'/>" << endl;
00124 break;
00125 }
00126 case ty_uint8:
00127 {
00128 out << "<uint sz='8'/>" << endl;
00129 break;
00130 }
00131 case ty_uint16:
00132 {
00133 out << "<uint sz='16'/>" << endl;
00134 break;
00135 }
00136 case ty_uint32:
00137 {
00138 out << "<uint sz='32'/>" << endl;
00139 break;
00140 }
00141 case ty_uint64:
00142 {
00143 out << "<uint sz='64'/>" << endl;
00144 break;
00145 }
00146
00147 case ty_field:
00148 {
00149 out << "<field name='" << t->litValue.s << "'/>";
00150 break;
00151 }
00152
00153 case ty_tvar:
00154 {
00155 string name;
00156 if (tvP)
00157 name = tvP->tvName(t);
00158
00159 if (name == "'a")
00160 out << "<tvar name='alpha'/>" << endl;
00161 else if (name == "'b")
00162 out << "<tvar name='beta'/>" << endl;
00163 else if (name == "'c")
00164 out << "<tvar name='gamma'/>" << endl;
00165 else if (name == "'d")
00166 out << "<tvar name='delta'/>" << endl;
00167 else
00168 out << "<tvar name='alpha' num='"<< t->uniqueID << "'/>" << endl;
00169 break;
00170 }
00171
00172 case ty_kvar:
00173 {
00174 out << "<lKind k='var' num='"<< t->uniqueID << "'/>" << endl;
00175 break;
00176 }
00177
00178 case ty_dummy:
00179 {
00180 out << "<dummy/>" << endl;
00181 break;
00182 }
00183
00184 #ifdef KEEP_BF
00185 case ty_bitfield:
00186 {
00187 assert(false);
00188 break;
00189 }
00190 #endif
00191
00192 case ty_method:
00193 case ty_fn:
00194 {
00195 std::string s = (t->typeTag == ty_fn)?"fn":"method";
00196 assert(t->components.size() == 2);
00197 out << "<" << s << ">" << endl;
00198 out.more();
00199 out << "<tuple>" << endl;
00200 out.more();
00201 t->Args()->asXML(tvP, out);
00202 out.less();
00203 out << "</tuple>" << endl;
00204 t->Ret()->minimizeMutability()->asXML(tvP, out);
00205 out.less();
00206 out << "</" << s << ">" << endl;
00207 break;
00208 }
00209
00210 case ty_fnarg:
00211 {
00212 for (size_t i=0; i < t->components.size(); i++) {
00213 if (t->CompFlags(i) & COMP_BYREF) {
00214 out << " <byref> ";
00215 t->CompType(i)->asXML(tvP, out);
00216 out << "<byref> ";
00217 }
00218 else {
00219 t->CompType(i)->minimizeMutability()->asXML(tvP, out);
00220 }
00221 }
00222 break;
00223 }
00224
00225 case ty_tyfn:
00226 {
00227 assert(t->components.size() == 2);
00228 out << "<tyfn>" << endl;
00229 out.more();
00230 out << "<tuple>" << endl;
00231 out.more();
00232 t->Args()->asXML(tvP, out);
00233 out.less();
00234 out << "</tuple>" << endl;
00235 t->Ret()->minimizeMutability()->asXML(tvP, out);
00236 out.less();
00237 out << "</tyfn>" << endl;
00238 break;
00239 }
00240
00241 case ty_letGather:
00242 {
00243 assert(false);
00244 break;
00245 }
00246
00247 case ty_structv:
00248 case ty_structr:
00249 {
00250 out << "<struct inline='yes' name='" << printName(t->defAst) <<"'>" << endl;
00251 out.more();
00252 for (size_t i=0; i < t->typeArgs.size(); i++)
00253 t->TypeArg(i)->asXML(tvP, out);
00254 out.less();
00255 out << "</struct>" << endl;
00256 break;
00257 }
00258
00259 case ty_unionv:
00260 case ty_unionr:
00261 case ty_uvalv:
00262 case ty_uvalr:
00263 case ty_uconv:
00264 case ty_uconr:
00265 {
00266 out << "<union inline='yes' name='" << printName(t->myContainer) <<"'>" << endl;
00267 out.more();
00268 for (size_t i=0; i < t->typeArgs.size(); i++)
00269 t->TypeArg(i)->asXML(tvP, out);
00270 out.less();
00271 out << "</union>" << endl;
00272 break;
00273 }
00274
00275 case ty_typeclass:
00276 {
00277 out << "<typeclass name='" << printName(t->defAst) <<"'>" << endl;
00278 out.more();
00279 for (size_t i=0; i < t->typeArgs.size(); i++)
00280 t->TypeArg(i)->asXML(tvP, out);
00281 out.less();
00282 out << "</typeclass>" << endl;
00283 break;
00284 }
00285
00286 case ty_array:
00287 {
00288 out << "<array sz='" << t->arrLen->len <<"'>" << endl;
00289 out.more();
00290 t->Base()->asXML(tvP, out);
00291 out.less();
00292 out << "</array>" << endl;
00293 break;
00294 }
00295
00296 case ty_vector:
00297 {
00298 out << "<vector>" << endl;
00299 out.more();
00300 t->Base()->asXML(tvP, out);
00301 out.less();
00302 out << "</vector>" << endl;
00303 break;
00304 }
00305
00306 case ty_ref:
00307 {
00308 out << "<ref>" << endl;
00309 out.more();
00310 t->Base()->asXML(tvP, out);
00311 out.less();
00312 out << "</ref>" << endl;
00313 break;
00314 }
00315
00316 case ty_byref:
00317 {
00318 out << "<byref>" << endl;
00319 out.more();
00320 t->Base()->asXML(tvP, out);
00321 out.less();
00322 out << "</byref>" << endl;
00323 break;
00324 }
00325
00326 case ty_array_ref:
00327 {
00328 out << "<array-byref>" << endl;
00329 out.more();
00330 t->Base()->asXML(tvP, out);
00331 out.less();
00332 out << "</array-byref>" << endl;
00333 break;
00334 }
00335
00336 case ty_mbTop:
00337 case ty_mbFull:
00338 {
00339 out << ((t->typeTag == ty_mbFull) ? "<MBPAIR>" : "<mbpair>")
00340 << endl;
00341 out.more();
00342 t->Var()->asXML(tvP, out);
00343 t->Core()->asXML(tvP, out);
00344 out << ((t->typeTag == ty_mbFull) ? "</MBPAIR>" : "</mbpair>")
00345 << endl;
00346 break;
00347 }
00348
00349 case ty_mutable:
00350 {
00351 out << "<mutable>" << endl;
00352 out.more();
00353 t->Base()->asXML(tvP, out);
00354 out.less();
00355 out << "</mutable>" << endl;
00356 break;
00357 }
00358
00359 case ty_const:
00360 {
00361 out << "<const>" << endl;
00362 out.more();
00363 t->Base()->asXML(tvP, out);
00364 out.less();
00365 out << "</const>" << endl;
00366 break;
00367 }
00368
00369 case ty_pcst:
00370 {
00371 out << "<pcst>" << endl;
00372 out.more();
00373 t->CompType(0)->asXML(tvP, out);
00374 t->CompType(1)->asXML(tvP, out);
00375 out << "</pcst>" << endl;
00376 for (size_t i=0; i<t->components.size(); i++)
00377 t->CompType(i)->asXML(tvP, out);
00378 break;
00379 }
00380
00381 case ty_kfix:
00382 {
00383 if (t == Type::Kmono)
00384 out << "<lKind k='mono'/>" << endl;
00385 else if (t == Type::Kpoly)
00386 out << "<lKind k='poly'/>" << endl;
00387 else
00388 assert(false);
00389 break;
00390 }
00391
00392 case ty_exn:
00393 {
00394 out << "<exception/>" << endl;
00395 break;
00396 }
00397 }
00398 t->pMark--;
00399 }
00400
00401
00402 string
00403 Type::asXML(shared_ptr<TvPrinter> tvP)
00404 {
00405 std::stringstream ss;
00406 INOstream out(ss);
00407 asXML(tvP, out);
00408 return ss.str();
00409 }
00410
00411 static inline bool
00412 mustShowPred(shared_ptr<Typeclass> pred)
00413 {
00414 if ((Options::showAllTccs) ||
00415 (((pred->flags & TY_CT_SUBSUMED) == 0) &&
00416 ((pred->flags & TY_CT_SELF) == 0)))
00417 return true;
00418 else
00419 return false;
00420 }
00421
00422 void
00423 TypeScheme::asXML(shared_ptr<TvPrinter> tvP, INOstream &out)
00424 {
00425 normalize();
00426 shared_ptr<TCConstraints> _tcc = TCConstraints::make();
00427 out << "<TS>" << endl;
00428 out.more();
00429
00430 for (TypeSet::iterator itr_i = ftvs.begin();
00431 itr_i != ftvs.end(); ++itr_i)
00432 out << (*itr_i)->asXML(tvP);
00433
00434 if (tcc) {
00435 if (Options::showAllTccs)
00436 _tcc = tcc;
00437 else
00438 addConstraints(_tcc);
00439 }
00440
00441 out << "<CType>" << endl;
00442 out.more();
00443 if (_tcc->size()) {
00444 for (TypeSet::iterator itr = _tcc->begin();
00445 itr != _tcc->end(); ++itr)
00446 if (mustShowPred((*itr)))
00447 (*itr)->asXML(tvP);
00448 }
00449
00450 tau->asXML(tvP, out);
00451 out.less();
00452 out << "</CType>" << endl;
00453 out.less();
00454 out << "</TS>" << endl;
00455 }
00456
00457 std::string
00458 TypeScheme::asXML(shared_ptr<TvPrinter> tvP)
00459 {
00460 normalize();
00461 std::stringstream ss;
00462 INOstream out(ss);
00463 asXML(tvP, out);
00464 return ss.str();
00465 }
00466
00467 void
00468 Instance::asXML(INOstream &out)
00469 {
00470 ts->asXML(GC_NULL, out);
00471 }
00472
00473 std::string
00474 Instance::asXML()
00475 {
00476 return ts->asXML();
00477 }