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
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
00064 shared_ptr<AST>
00065 Type::asAST(const sherpa::LexLoc &loc,
00066 shared_ptr<TvPrinter> tvP)
00067 {
00068 shared_ptr<AST> ast = GC_NULL;
00069 shared_ptr<Type> t = getType();
00070
00071 if (t->pMark >= 1) {
00072 ast = AST::make(at_ident, loc);
00073 ast->s = tvP->tvName(t);
00074 ast->identType = id_tvar;
00075
00076 return ast;
00077 }
00078 else {
00079 t->pMark++;
00080 }
00081
00082 switch(t->typeTag) {
00083 case ty_unit:
00084 {
00085 ast = AST::make(at_primaryType, loc);
00086 ast->s = "unit";
00087 break;
00088 }
00089 case ty_bool:
00090 {
00091 ast = AST::make(at_primaryType, loc);
00092 ast->s = "bool";
00093 break;
00094 }
00095 case ty_char:
00096 {
00097 ast = AST::make(at_primaryType, loc);
00098 ast->s = "char";
00099 break;
00100 }
00101 case ty_string:
00102 {
00103 ast = AST::make(at_primaryType, loc);
00104 ast->s = "string";
00105 break;
00106 }
00107 case ty_int8:
00108 {
00109 ast = AST::make(at_primaryType, loc);
00110 ast->s = "int8";
00111 break;
00112 }
00113 case ty_int16:
00114 {
00115 ast = AST::make(at_primaryType, loc);
00116 ast->s = "int16";
00117 break;
00118 }
00119 case ty_int32:
00120 {
00121 ast = AST::make(at_primaryType, loc);
00122 ast->s = "int32";
00123 break;
00124 }
00125 case ty_int64:
00126 {
00127 ast = AST::make(at_primaryType, loc);
00128 ast->s = "int64";
00129 break;
00130 }
00131 case ty_uint8:
00132 {
00133 ast = AST::make(at_primaryType, loc);
00134 ast->s = "uint8";
00135 break;
00136 }
00137 case ty_uint16:
00138 {
00139 ast = AST::make(at_primaryType, loc);
00140 ast->s = "uint16";
00141 break;
00142 }
00143 case ty_uint32:
00144 {
00145 ast = AST::make(at_primaryType, loc);
00146 ast->s = "uint32";
00147 break;
00148 }
00149 case ty_uint64:
00150 {
00151 ast = AST::make(at_primaryType, loc);
00152 ast->s = "uint64";
00153 break;
00154 }
00155 case ty_word:
00156 {
00157 ast = AST::make(at_primaryType, loc);
00158 ast->s = "word";
00159 break;
00160 }
00161 case ty_float:
00162 {
00163 ast = AST::make(at_primaryType, loc);
00164 ast->s = "float";
00165 break;
00166 }
00167 case ty_double:
00168 {
00169 ast = AST::make(at_primaryType, loc);
00170 ast->s = "double";
00171 break;
00172 }
00173 case ty_quad:
00174 {
00175 ast = AST::make(at_primaryType, loc);
00176 ast->s = "quad";
00177 break;
00178 }
00179 case ty_tvar:
00180 {
00181 ast = AST::make(at_ident, loc);
00182 ast->s = tvP->tvName(t);
00183 ast->identType = id_tvar;
00184 break;
00185 }
00186
00187 case ty_mbTop:
00188 {
00189 ast = t->Core()->asAST(loc, tvP);
00190 break;
00191 }
00192
00193 case ty_mbFull:
00194 {
00195 if(t->Var()->isMutable())
00196 ast = t->Core()->maximizeMutability()->asAST(loc, tvP);
00197 else
00198 ast = t->Core()->asAST(loc, tvP);
00199
00200 break;
00201 }
00202
00203 case ty_dummy:
00204 {
00205 ast = AST::make(at_dummyType, loc);
00206 break;
00207 }
00208
00209
00210 #ifdef KEEP_BF
00211 case ty_bitfield:
00212 {
00213 shared_ptr<AST> typ = t->CompType(0)->asAST(loc, tvP);
00214 shared_ptr<AST> intLit = AST::make(at_intLiteral, loc);
00215 mpz_init_set_ui(intLit->litValue.i, t->Isize);
00216 intLit->litBase = 10;
00217 ast = AST::make(at_bitfieldType, loc, typ, intLit);
00218 break;
00219 }
00220 #endif
00221
00222 case ty_method:
00223 {
00224 assert(t->components.size() == 2);
00225 shared_ptr<AST> arg = t->Args()->asAST(loc, tvP);
00226 shared_ptr<AST> ret = t->Ret()->asAST(loc, tvP);
00227 ast = AST::make(at_methType, loc, arg, ret);
00228 break;
00229 }
00230
00231 case ty_fn:
00232 {
00233 assert(t->components.size() == 2);
00234 shared_ptr<AST> arg = t->Args()->asAST(loc, tvP);
00235 shared_ptr<AST> ret = t->Ret()->asAST(loc, tvP);
00236 ast = AST::make(at_fn, loc, arg, ret);
00237 break;
00238 }
00239
00240 case ty_fnarg:
00241 {
00242 ast = AST::make(at_fnargVec, loc);
00243 for (size_t i=0; i < t->components.size(); i++) {
00244 shared_ptr<AST> arg = t->CompType(i)->asAST(loc, tvP);
00245 if (t->CompFlags(i) & COMP_BYREF)
00246 arg = AST::make(at_byRefType, arg->loc, arg);
00247
00248 ast->children.push_back(arg);
00249 }
00250 break;
00251 }
00252
00253 case ty_tyfn:
00254 {
00255 assert(false);
00256 break;
00257 }
00258
00259 case ty_structv:
00260 case ty_structr:
00261 case ty_unionv:
00262 case ty_unionr:
00263 {
00264 ast = t->defAst->Use();
00265
00266
00267
00268 if (t->typeArgs.size() > 0) {
00269 ast = AST::make(at_typeapp, loc, ast);
00270 for (size_t i=0; i < t->typeArgs.size(); i++)
00271 ast->children.push_back(t->TypeArg(i)->asAST(loc, tvP));
00272 }
00273 break;
00274 }
00275
00276 case ty_field:
00277 {
00278 shared_ptr<AST> fld = AST::make(at_ident, loc);
00279 fld->s = t->litValue.s;
00280 ast = AST::make(at_fieldType, fld->loc, fld);
00281 break;;
00282 }
00283
00284 case ty_uconv:
00285 case ty_uconr:
00286 case ty_uvalv:
00287 case ty_uvalr:
00288 {
00289 ast = t->myContainer->Use();
00290
00291
00292 if (t->typeArgs.size()) {
00293 ast = AST::make(at_typeapp, loc, ast);
00294 for (size_t i=0; i < t->typeArgs.size(); i++)
00295 ast->children.push_back(t->TypeArg(i)->asAST(loc, tvP));
00296 }
00297 break;
00298 }
00299
00300 case ty_typeclass:
00301 assert(false);
00302
00303 case ty_array:
00304 {
00305 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00306 shared_ptr<AST> intLit = AST::make(at_intLiteral, loc);
00307 intLit->litValue.i = t->arrLen->len;
00308 intLit->litBase = 10;
00309 intLit->s = intLit->litValue.i.asString(10);
00310 ast = AST::make(at_arrayType, loc, typ, intLit);
00311 break;
00312 }
00313
00314 case ty_vector:
00315 {
00316 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00317 ast = AST::make(at_vectorType, loc, typ);
00318 break;
00319 }
00320
00321 case ty_ref:
00322 {
00323 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00324 ast = AST::make(at_boxedType, loc, typ);
00325 break;
00326 }
00327
00328 case ty_byref:
00329 {
00330 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00331 ast = AST::make(at_byRefType, loc, typ);
00332 break;
00333 }
00334
00335 case ty_array_ref:
00336 {
00337 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00338 ast = AST::make(at_arrayRefType, loc, typ);
00339 break;
00340 }
00341
00342 case ty_mutable:
00343 {
00344 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00345 ast = AST::make(at_mutableType, loc, typ);
00346 break;
00347 }
00348
00349 case ty_const:
00350 {
00351 shared_ptr<AST> typ = t->Base()->asAST(loc, tvP);
00352 ast = AST::make(at_constType, loc, typ);
00353 break;
00354 }
00355
00356 case ty_exn:
00357 {
00358 ast = AST::make(at_exceptionType, loc);
00359 break;
00360 }
00361
00362 case ty_letGather:
00363 case ty_pcst:
00364 case ty_kvar:
00365 case ty_kfix:
00366 assert(false);
00367 break;
00368 }
00369
00370 t->pMark--;
00371 assert(ast);
00372 return ast;
00373 }