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
00065
00066 #include <assert.h>
00067 #include <stdint.h>
00068 #include <stdlib.h>
00069 #include <dirent.h>
00070 #include <fstream>
00071 #include <iostream>
00072 #include <string>
00073 #include <stack>
00074
00075 #include "Options.hxx"
00076 #include "AST.hxx"
00077 #include "Environment.hxx"
00078 #include "Symtab.hxx"
00079 #include "inter-pass.hxx"
00080 #include "MixFix.hxx"
00081
00082 using namespace std;
00083 using namespace boost;
00084 using namespace sherpa;
00085
00086
00087
00088
00089
00090 static bool
00091 warnUnresRef(std::ostream& errStream,
00092 shared_ptr<AST> mod,
00093 shared_ptr<ASTEnvironment > env)
00094 {
00095 bool errorFree = true;
00096
00097 assert(mod->astType == at_module);
00098 for (size_t c=0; c < mod->children.size(); c++) {
00099 shared_ptr<AST> ast = mod->child(c);
00100 switch(ast->astType) {
00101 case at_declunion:
00102 case at_declstruct:
00103 case at_proclaim:
00104 {
00105
00106
00107 if (ast->flags & PROCLAIM_IS_INTERNAL)
00108 break;
00109
00110
00111
00112 if (ast->flags & DEF_IS_EXTERNAL)
00113 break;
00114
00115
00116
00117
00118
00119
00120
00121 shared_ptr<AST> binding = env->getBinding(ast->child(0)->s);
00122 if (binding->isDecl) {
00123 errStream << ast->loc << ": WARNING: "
00124 << "Local declaration of " << ast->child(0)->s
00125 << " found here, but no definition found."
00126 << std::endl;
00127
00128 if (Options::Wall)
00129 errorFree = false;
00130 break;
00131 }
00132 }
00133 default:
00134 break;
00135 }
00136 }
00137 return errorFree;
00138 }
00139
00140 static shared_ptr<UocInfo>
00141 findInterface(std::ostream& errStream, shared_ptr<AST> ifAst)
00142 {
00143 shared_ptr<UocInfo> iface=GC_NULL;
00144
00145 UocMap::iterator itr = UocInfo::ifList.find(ifAst->s);
00146 if (itr != UocInfo::ifList.end()) {
00147 shared_ptr<UocInfo> thisIface = itr->second;
00148 iface = thisIface;
00149 }
00150
00151 if (!iface) {
00152 errStream << ifAst->loc << ": "
00153 << "Internal Compiler Error. "
00154 << "Interface " << ifAst->s
00155 << " has NOT been processed"
00156 << std::endl;
00157 return GC_NULL;
00158 }
00159
00160 if (!iface->env || !iface->gamma || !iface->instEnv) {
00161 errStream << ifAst->loc << ": "
00162 << "Internal Compiler Error. "
00163 << "Interface " << ifAst->s
00164 << " has at least one NULL environment"
00165 << std::endl;
00166 return GC_NULL;
00167 }
00168
00169 ifAst->envs.env = iface->env;
00170 ifAst->envs.gamma = iface->gamma;
00171 ifAst->envs.instEnv = iface->instEnv;
00172
00173 return iface;
00174 }
00175
00176 #if 0
00177
00178
00179 static void
00180 showEnv(std::ostream& errStream,
00181 shared_ptr<ASTEnvironment > env)
00182 {
00183 for (ASTEnvironment::iterator itr = env->begin();
00184 itr != env->end(); ++ itr) {
00185 std::string name = itr->first;
00186 shared_ptr<Binding<AST> > bdng = itr->second;
00187
00188 errStream << name << ": "
00189 << ((bdng->flags & BF_COMPLETE)? "Complete" : "Incomplete")
00190 << std::endl;
00191 }
00192
00193 if(env->parent) {
00194 errStream << " -- " << std::endl;
00195 showEnv(errStream, env->parent);
00196 }
00197 }
00198 #endif
00199
00200 static void
00201 aliasPublicBindings(const std::string& idName,
00202 shared_ptr<ASTEnvironment > aliasEnv,
00203 shared_ptr<ASTEnvironment > fromEnv,
00204 shared_ptr<ASTEnvironment > toEnv)
00205 {
00206 for (ASTEnvironment::iterator itr = fromEnv->begin();
00207 itr != fromEnv->end(); ++itr) {
00208 std::string s = itr->first;
00209 shared_ptr<Binding<AST> > bdng = itr->second;
00210
00211 if (bdng->flags & BF_PRIVATE)
00212 continue;
00213
00214 shared_ptr<AST> ast = bdng->val;
00215
00216 if (aliasEnv && aliasEnv->getBinding(ast->fqn.asString()))
00217 continue;
00218
00219
00220 if (idName.size())
00221 s = idName + FQName::LocalBindingSep + s;
00222
00223 toEnv->addBinding(s, ast);
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 toEnv->setFlags(s, BF_PRIVATE|BF_COMPLETE);
00245 }
00246 }
00247
00248 static bool
00249 importIfBinding(std::ostream& errStream,
00250 shared_ptr<ASTEnvironment > aliasEnv,
00251 shared_ptr<AST> ifName)
00252 {
00253 if (!findInterface(errStream, ifName))
00254 return false;
00255
00256 std::string canonicalIfName = ifName->s;
00257
00258
00259 shared_ptr<AST> ifAst = aliasEnv->getBinding(canonicalIfName);
00260 if (ifAst) {
00261
00262
00263 ifName->envs.env = ifAst->envs.env;
00264 ifName->envs.gamma = ifAst->envs.gamma;
00265 ifName->envs.instEnv = ifAst->envs.instEnv;
00266 }
00267
00268 aliasEnv->addBinding(canonicalIfName, ifName);
00269
00270
00271
00272 shared_ptr<ASTEnvironment > dupEnv =
00273 ASTEnvironment::make(ifName->envs.env->uocName);
00274
00275 for (ASTEnvironment::iterator itr = ifName->envs.env->begin();
00276 itr != ifName->envs.env->end(); ++itr) {
00277 std::string s = itr->first;
00278 shared_ptr<Binding<AST> > bdng = itr->second;
00279
00280 if (bdng->flags & BF_PRIVATE) {
00281 continue;
00282 }
00283
00284 shared_ptr<AST> ast = bdng->val;
00285
00286 dupEnv->addBinding(s, ast);
00287 dupEnv->setFlags(s, bdng->flags);
00288 }
00289
00290 ifName->envs.env = dupEnv;
00291 return true;
00292 }
00293
00294 static bool
00295 providing(shared_ptr<ASTEnvironment > aliasEnv, const FQName& fqn)
00296 {
00297
00298 shared_ptr<AST> ifName = aliasEnv->getBinding(fqn.iface);
00299
00300
00301
00302
00303 if (!ifName)
00304 return true;
00305
00306 shared_ptr<ASTEnvironment > pubEnv = ifName->envs.env;
00307
00308
00309 shared_ptr<Binding<AST> > bdng = pubEnv->doGetBinding(fqn.ident);
00310
00311 assert(bdng);
00312
00313
00314 return (bdng->flags & BF_PROVIDING);
00315 }
00316
00317 #if 0
00318 bool
00319 makeLocalAlias(shared_ptr<ASTEnvironment > fromEnv,
00320 std::string fromName,
00321 shared_ptr<ASTEnvironment > toEnv,
00322 const std::string& toPfx,
00323 shared_ptr<AST> toIdent)
00324 {
00325 shared_ptr<Binding<AST> > bndg = fromEnv->doGetBinding(fromName);
00326
00327 if (bndg->flags & BF_PRIVATE)
00328 return false;
00329
00330 shared_ptr<AST> ast = bndg->val;
00331
00332 std::string s = toIdent->s;
00333 if (toPfx.size())
00334 s = toPfx + FQName::sep + s;
00335
00336 toEnv->addBinding(s, ast);
00337 toEnv->setFlags(s, BF_PRIVATE|BF_COMPLETE);
00338
00339 return true;
00340 }
00341 #endif
00342
00343 static void
00344 bindIdentDef(shared_ptr<AST> ast, shared_ptr<ASTEnvironment > env,
00345 IdentType identType, shared_ptr<AST> currLB,
00346 ResolverFlags rflags)
00347 {
00348 if (ast->isIdentType(id_tvar)) {
00349 env->addDefBinding(ast->s, ast);
00350 env->setFlags(ast->s, BF_COMPLETE | BF_NO_MERGE);
00351
00352 assert(currLB);
00353 ast->tvarLB = currLB;
00354 }
00355 else
00356 env->addBinding(ast->s, ast);
00357
00358
00359
00360
00361
00362
00363
00364 if (ast->identType != id_unresolved)
00365 assert(ast->isIdentType(identType));
00366 else
00367 ast->identType = identType;
00368
00369
00370
00371
00372
00373
00374 }
00375
00376 static void
00377 markComplete(shared_ptr<ASTEnvironment > env)
00378 {
00379 for (ASTEnvironment::iterator itr = env->begin();
00380 itr != env->end(); ++itr)
00381 itr->second->flags |= BF_COMPLETE;
00382 }
00383
00384 #define REWRITE_AST_IN_PLACE(_to, _from) \
00385 do { \
00386 shared_ptr<AST> to = (_to); \
00387 shared_ptr<AST> from = (_from); \
00388 \
00389 to->s = from->s; \
00390 to->astType = from->astType; \
00391 to->identType = from->identType; \
00392 to->flags = from->flags; \
00393 to->children = from->children; \
00394 to->fqn = from->fqn; \
00395 } while (0)
00396
00397
00398
00399 #define RESOLVE(ast,env,lamLevel,mode,identType,currLB,flags) \
00400 do { \
00401 bool answer = \
00402 resolve(errStream, (ast), aliasEnv, (env), (lamLevel), \
00403 (mode), (identType), (currLB), (flags)); \
00404 if (answer == false) \
00405 errorFree = false; \
00406 } while (0)
00407
00408
00432 bool
00433 resolve(std::ostream& errStream,
00434 shared_ptr<AST> ast,
00435 shared_ptr<ASTEnvironment > aliasEnv,
00436 shared_ptr<ASTEnvironment > env,
00437 shared_ptr<ASTEnvironment > lamLevel,
00438 ResolutionMode mode,
00439 IdentType identType,
00440 shared_ptr<AST> currLB,
00441 ResolverFlags flags)
00442 {
00443 bool errorFree = true;
00444
00445
00446
00447 ast->envs.env = env;
00448
00449
00450
00451
00452
00453
00454
00455
00456 switch(ast->astType) {
00457
00458 case at_Null:
00459 case at_boxedCat:
00460 case at_unboxedCat:
00461 case at_opaqueCat:
00462 case agt_category:
00463 case at_AnyGroup:
00464 case agt_literal:
00465 case agt_tvar:
00466 case agt_var:
00467 case agt_definition:
00468 case agt_type:
00469 case agt_expr:
00470 case agt_expr_or_define:
00471 case agt_eform:
00472 case agt_type_definition:
00473
00474 case agt_value_definition:
00475 case agt_uselhs:
00476 case at_letbindings:
00477 case at_loopbindings:
00478 case at_loopbinding:
00479 case agt_CompilationUnit:
00480 case agt_tc_definition:
00481 case agt_if_definition:
00482 case agt_ow:
00483 case at_localFrame:
00484 case at_frameBindings:
00485 case at_identList:
00486 case agt_qtype:
00487 case agt_fielditem:
00488 case at_defrepr:
00489
00490
00491
00492
00493 case at_oc_closed:
00494 case at_oc_open:
00495 case agt_openclosed:
00496 case at_reprctrs:
00497 case at_reprctr:
00498 case at_reprrepr:
00499 case at_docString:
00500 case at_letGather:
00501 case agt_ucon:
00502 {
00503 errStream << ast->loc << ": Internal Compiler Error. "
00504 << "Function resolve, unexpected astType: "
00505 << ast->tagName()
00506 << std::endl;
00507
00508 errorFree = false;
00509 break;
00510 }
00511
00512 case at_boolLiteral:
00513 case at_charLiteral:
00514 case at_intLiteral:
00515 case at_floatLiteral:
00516 case at_stringLiteral:
00517 break;
00518
00519 case at_ident:
00520 {
00521
00522
00523 if (!ast->fqn.isInitialized()) {
00524 if (ast->isGlobal()) {
00525 ast->fqn = FQName(env->uocName, ast->s);
00526 ast->flags |= ID_IS_PRIVATE;
00527 }
00528 else
00529 ast->fqn = FQName(FQ_NOIF, ast->s);
00530 }
00531
00532 switch(mode) {
00533
00534
00535
00536
00537
00538
00539
00540
00541 case DEF_MODE:
00542 {
00543 assert(env);
00544 assert(identType != id_unresolved);
00545
00546 shared_ptr<AST> sym = env->getBinding(ast->s);
00547
00548 if (sym) {
00549 if (sym->isDecl) {
00550 if ((sym->fqn.iface != ast->fqn.iface) &&
00551 !providing(aliasEnv, sym->fqn)) {
00552
00553
00554
00555
00556 errStream << ast->loc << ": "
00557 << ast->s
00558 << " aliases an interface symbol that"
00559 << " is not being provided."
00560 << std::endl;
00561 errorFree = false;
00562 break;
00563
00564 }
00565
00566 ast->identType = identType;
00567 if (!ast->isIdentType(sym->identType)) {
00568 errStream << sym->loc << ": "
00569 << ast->s << " is declared here as "
00570 << identTypeToString(sym->identType)
00571 << std::endl
00572 << ast->loc
00573 << " and defined here as "
00574 << identTypeToString(identType)
00575 << std::endl;
00576 errorFree = false;
00577 break;
00578 }
00579
00580 sym->identType = ast->identType;
00581 sym->defn = ast;
00582 ast->decl = sym;
00583 env->removeBinding(ast->s);
00584
00585 if (sym->externalName.size()) {
00586 ast->externalName = sym->externalName;
00587 }
00588 }
00589 else if (ast->isGlobal()) {
00590 errStream << ast->loc << ": Redefinition of Symbol "
00591 << ast->s << ". Already defined at "
00592 << sym->loc << "." << std::endl;
00593 errorFree = false;
00594 break;
00595 }
00596 }
00597
00598 assert(!ast->isDecl);
00599 bindIdentDef(ast, env, identType, currLB, flags);
00600 break;
00601 }
00602
00603 case DECL_MODE:
00604 {
00605 assert(env);
00606 assert(identType != id_unresolved);
00607
00608 ast->isDecl = true;
00609
00610 shared_ptr<AST> sym = env->getBinding(ast->s);
00611
00612 if (sym) {
00613 if (!sym->isDecl) {
00614
00615 if ((flags & RSLV_NO_RESOLVE_DECL) == 0) {
00616 errStream << ast->loc << ": Symbol "
00617 << ast->s << " already defined at "
00618 << sym->loc << "." << std::endl;
00619 errorFree = false;
00620 break;
00621 }
00622 }
00623
00624 if (!sym->isIdentType(identType)) {
00625 errStream << sym->loc << ": "
00626 << ast->s << " is declared/defined here as "
00627 << identTypeToString(sym->identType)
00628 << std::endl
00629 << ast->loc
00630 << " and here as "
00631 << identTypeToString(identType)
00632 << std::endl;
00633 errorFree = false;
00634 break;
00635 }
00636 ast->identType = sym->identType;
00637
00638 if (ast->externalName.size()) {
00639 assert(ast->flags & DEF_IS_EXTERNAL);
00640 if (sym->externalName.size()) {
00641 if (sym->externalName != ast->externalName) {
00642 errStream << ast->loc << ": "
00643 << ast->s << " Identifier was previously "
00644 << " declared with a different external name. "
00645 << "Name here: " << ast->externalName
00646 << "Previous name: " << sym->externalName
00647 << std::endl;
00648 errorFree = false;
00649 break;
00650 }
00651 }
00652 else {
00653 sym->externalName = ast->externalName;
00654 }
00655 }
00656 else {
00657 if (sym->externalName.size()) {
00658 ast->externalName = sym->externalName;
00659 }
00660 }
00661
00662
00663 }
00664 else
00665 bindIdentDef(ast, env, identType, currLB, flags);
00666 break;
00667 }
00668
00669 case USE_MODE:
00670 {
00671 assert(env);
00672 assert((flags & RSLV_NO_CHK_USE_TYPE) ||
00673 (identType != id_unresolved));
00674
00675
00676
00677
00678 ast->symbolDef = env->getBinding(ast->s);
00679
00680
00681
00682
00683 if (!ast->symbolDef) {
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704 if (ast->isIdentType(id_tvar) &&
00705 ((flags & RSLV_NEW_TV_OK) ||
00706 (ast->flags & TVAR_POLY_SPECIAL))) {
00707 bindIdentDef(ast, env, identType, currLB, flags);
00708 ast->symbolDef = ast;
00709
00710
00711
00712
00713 }
00714 else {
00715 errStream << ast->loc << ": Identifier `"
00716 << ast->s << "' used here, Undefined."
00717 << std::endl;
00718
00719
00720
00721
00722
00723 errorFree = false;
00724 break;
00725 }
00726 }
00727
00728 assert(ast->symbolDef);
00729
00730
00731
00732
00733 if ((identType == id_block) &&
00734 !env->getBinding(ast->s, lamLevel)) {
00735 errStream << ast->loc << ": Escape to "
00736 << ast->s << "' must not cross lambda boundary."
00737 << std::endl;
00738 errorFree = false;
00739 break;
00740 }
00741
00742 shared_ptr<AST> def = ast->symbolDef;
00743
00744 if ((flags & RSLV_USE_ONLY_PUBLIC) &&
00745 ((env->getFlags(ast->s) & BF_PRIVATE))) {
00746 errStream << ast->loc << ": Identifier `"
00747 << ast->s << "' used here, But NO public definition "
00748 << "found." << std::endl;
00749 errorFree = false;
00750 break;
00751 }
00752
00753 if (((flags & RSLV_NO_CHK_USE_TYPE) == 0) &&
00754 (!def->isIdentType(identType))) {
00755 errStream << ast->loc << ": " << identTypeToString(identType)
00756 << " `" << ast->s << "' Undefined"
00757 << " [But there is a "
00758 << identTypeToString(def->identType)
00759 << " defined]" << std::endl;
00760 errorFree = false;
00761 }
00762
00763 bool ICRviolation = false;
00764
00765
00766
00767
00768
00769
00770 if (((flags & RSLV_INCOMPLETE_NO_CHK) == 0) &&
00771 ((env->getFlags(ast->s) & BF_COMPLETE) == 0)) {
00772 if ((flags & RSLV_INCOMPLETE_OK) == 0) {
00773
00774
00775 ICRviolation = true;
00776 }
00777 else {
00778 if (def->isIdentType(id_value)) {
00779 assert(lamLevel);
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795 if (!lamLevel->getBinding(ast->s))
00796 ICRviolation = true;
00797 }
00798 }
00799 }
00800
00801 if (ICRviolation) {
00802 errStream << ast->loc << ": Usage of Identifier `"
00803 << ast->s << "' Violates Incompleteness Restriction."
00804 << std::endl;
00805 errorFree = false;
00806 }
00807
00808 if (def->flags & ID_FOR_USWITCH) {
00809 if ((flags & RSLV_SWITCHED_ID_OK) == 0) {
00810 errStream << ast->loc << ": The identifier `"
00811 << ast->s << "' can only appear to the left of a `.'"
00812 << std::endl;
00813 errorFree = false;
00814 }
00815 else if (flags & RSLV_WITHIN_CATCH_MC) {
00816 errStream << ast->loc << ": The identifier `"
00817 << ast->s << "' cannot be used while"
00818 << " catching multiple exceptions."
00819 << std::endl;
00820 errorFree = false;
00821 }
00822 }
00823
00824 ast->identType = def->identType;
00825 ast->flags |= def->flags;
00826 ast->flags |= def->flags;
00827 ast->flags &= ~MASK_FLAGS_FROM_USE;
00828 ast->externalName = def->externalName;
00829
00830
00831 if (def->isIdentType(id_tvar)) {
00832 assert(currLB);
00833
00834 shared_ptr<AST> thisLB = GC_NULL;
00835 if (def->tvarLB->envs.env->isAncestor(currLB->envs.env))
00836 thisLB = currLB;
00837 else
00838 thisLB = def->tvarLB;
00839
00840 while (thisLB->flags & LBS_PROCESSED) {
00841 thisLB = thisLB->parentLB;
00842 assert(thisLB);
00843 }
00844
00845 def->tvarLB = thisLB;
00846 }
00847
00848
00849
00850
00851
00852 break;
00853 }
00854
00855 default:
00856 {
00857 assert(false);
00858 break;
00859 }
00860 }
00861
00862 break;
00863 }
00864
00865 case at_usesel:
00866 {
00906
00914
00915 shared_ptr<AST> lhs = ast->child(0);
00916 {
00917
00918
00919
00920
00921 if (lhs->astType == at_usesel) {
00922 RESOLVE(lhs, env, lamLevel, USE_MODE, id_interface,
00923 GC_NULL, flags);
00924
00925 if (!errorFree)
00926 break;
00927
00928
00929 assert(lhs->astType == at_ident);
00930 }
00931 }
00932
00933 RESOLVE(lhs, env, lamLevel, USE_MODE, idc_usesel_lhs,
00934 GC_NULL, flags);
00935
00936 if (!errorFree)
00937 break;
00938
00939 if (lhs->identType == id_interface) {
00940 shared_ptr<AST> iface = lhs;
00941
00942 stringstream lookupStream;
00943 lookupStream << ":" << iface->s << ":";
00944 std::string lookupName = lookupStream.str();
00945 shared_ptr<AST> ifNameAst= aliasEnv->getBinding(lookupName);
00946 if (!ifNameAst) {
00947 errStream << ast->loc << ": "
00948 << "Identifier `"
00949 << iface->s
00950 << "' is undefined. Are you missing the name on an import statement?"
00951 << std::endl;
00952 errorFree = false;
00953 break;
00954 }
00955
00956 std::string ifName = ifNameAst->s;
00957 assert(ifName != "");
00958
00959 shared_ptr<ASTEnvironment > ifenv = iface->symbolDef->envs.env;
00960
00961 if (!ifenv) {
00962 errStream << ast->loc << ": "
00963 << "Internal Compiler Error. "
00964 << "Interface " << ifName
00965 << " needed by "<< iface->s << " has a NULL environment"
00966 << std::endl;
00967 errorFree = false;
00968 break;
00969 }
00970
00971 FQName importedFQN = FQName(ifName, ast->child(1)->s);
00972
00973 ast->s = lhs->s + FQName::LocalBindingSep + ast->child(1)->s;
00974 ast->astType = at_ident;
00975 ast->identType = ast->child(1)->identType;
00976 ast->flags = ast->child(1)->flags;
00977 ast->flags |= ID_IS_GLOBAL;
00978
00979 ast->children.clear();
00980
00981
00982 RESOLVE(ast, env, lamLevel, mode, identType, currLB,
00983 (flags & (~RSLV_BIND_PUBLIC)));
00984
00985 ast->fqn = importedFQN;
00986 }
00987 else if ((lhs->identType == id_struct) ||
00988 (lhs->identType == id_object)) {
00989 ast->s = lhs->s + "." + ast->child(1)->s;
00990 ast->astType = at_ident;
00991 ast->identType = ast->child(1)->identType;
00992 ast->flags = ast->child(1)->flags;
00993 ast->flags |= ID_IS_GLOBAL;
00994
00995 ast->children.clear();
00996
00997 RESOLVE(ast, env, lamLevel, mode, identType, currLB,
00998 flags);
00999
01000 break;
01001 }
01002 else {
01003 errStream << ast->loc << ": "
01004 << "Required struct name or interface name to"
01005 << " the left of this dot."
01006 << std::endl;
01007 errorFree = false;
01008 break;
01009 }
01010
01011 break;
01012 }
01013
01014 case at_interface:
01015 {
01016 flags |= RSLV_IS_INTERFACE;
01017
01018
01019 for (size_t c = 1; c < ast->children.size(); c++)
01020 RESOLVE(ast->child(c), env, lamLevel, DEF_MODE, identType,
01021 GC_NULL, flags);
01022
01023
01024
01025
01026
01027 break;
01028 }
01029
01030 case at_module:
01031 {
01032
01033 for (size_t c = 0; c < ast->children.size(); c++)
01034 RESOLVE(ast->child(c), env, lamLevel, DEF_MODE, identType,
01035 GC_NULL, flags);
01036
01037 if ((flags & RSLV_NO_RESOLVE_DECL) == 0)
01038 CHKERR(errorFree, warnUnresRef(errStream, ast, env));
01039
01040 break;
01041 }
01042
01043 case at_defunion:
01044 {
01045 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01046 ast->envs.env = tmpEnv;
01047
01048 shared_ptr<AST> category = ast->child(2);
01049
01050
01051 RESOLVE(ast->child(0), tmpEnv, lamLevel, DEF_MODE,
01052 id_union, ast,
01053 (flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK)) | RSLV_BIND_PUBLIC);
01054 if (category->astType == at_boxedCat)
01055 tmpEnv->setFlags(ast->child(0)->s, BF_COMPLETE);
01056
01057
01058 RESOLVE(ast->child(1), tmpEnv, lamLevel, DEF_MODE,
01059 id_tvar, ast,
01060 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01061
01062
01063
01064
01065 RESOLVE(ast->child(3), tmpEnv, lamLevel, NULL_MODE,
01066 id_unresolved, ast,
01067 ((flags & (~RSLV_NEW_TV_OK)) & (~RSLV_INCOMPLETE_OK)) | RSLV_WITHIN_DEFUNION);
01068
01069
01070
01071 RESOLVE(ast->child(4), tmpEnv, lamLevel, DEF_MODE,
01072 idc_uctor, ast,
01073 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01074
01075
01076 RESOLVE(ast->child(5), tmpEnv, lamLevel, USE_MODE,
01077 idc_type, ast,
01078 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01079
01080 env->mergeBindingsFrom(tmpEnv);
01081 break;
01082 }
01083
01084 case at_defstruct:
01085 case at_defobject:
01086 case at_defexception:
01087 {
01088 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01089 ast->envs.env = tmpEnv;
01090
01091 shared_ptr<AST> category = ast->child(2);
01092 shared_ptr<AST> fields = ast->child(4);
01093
01094 if (ast->astType == at_defexception &&
01095 category->astType != at_boxedCat) {
01096 errStream << ast->loc << ": "
01097 << "exception " << ast->child(0)->s
01098 << ": Exception definitions must be boxed."
01099 << std::endl;
01100 errorFree = false;
01101 }
01102
01103 IdentType identType;
01104 switch(ast->astType) {
01105 case at_defstruct:
01106 identType = id_struct;
01107 break;
01108 case at_defobject:
01109 identType = id_object;
01110 break;
01111 case at_defexception:
01112 identType = (fields->children.size() == 0) ? id_ucon0 : id_ucon;
01113 break;
01114 }
01115
01116
01117 RESOLVE(ast->child(0), tmpEnv, lamLevel, DEF_MODE,
01118 identType, ast,
01119 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK) | RSLV_BIND_PUBLIC);
01120 if (category->astType == at_boxedCat)
01121 tmpEnv->setFlags(ast->child(0)->s, BF_COMPLETE);
01122
01123
01124 RESOLVE(ast->child(1), tmpEnv, lamLevel, DEF_MODE,
01125 id_tvar, ast,
01126 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01127
01128
01129
01130
01131 RESOLVE(ast->child(3), tmpEnv, lamLevel, NULL_MODE,
01132 id_unresolved, ast,
01133 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01134
01135
01136 RESOLVE(ast->child(4), tmpEnv, lamLevel, DEF_MODE,
01137 id_field, ast,
01138 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01139
01140
01141 RESOLVE(ast->child(5), tmpEnv, lamLevel, USE_MODE,
01142 idc_type, ast,
01143 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01144
01145 env->mergeBindingsFrom(tmpEnv);
01146 break;
01147 }
01148
01149 case at_declunion:
01150 case at_declstruct:
01151 case at_declrepr:
01152 {
01153 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01154 ast->envs.env = tmpEnv;
01155
01156 IdentType it = ((ast->astType == at_declstruct) ?
01157 id_struct : id_union);
01158
01159
01160 RESOLVE(ast->child(0), tmpEnv, lamLevel, DECL_MODE,
01161 it, ast,
01162 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK)
01163 | RSLV_BIND_PUBLIC);
01164
01165
01166 RESOLVE(ast->child(1), tmpEnv, lamLevel, DEF_MODE,
01167 id_tvar, ast,
01168 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01169
01170
01171
01172
01173
01174
01175 RESOLVE(ast->child(5), tmpEnv, lamLevel, USE_MODE,
01176 idc_type, ast,
01177 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01178
01179 env->mergeBindingsFrom(tmpEnv);
01180 break;
01181 }
01182
01183 case at_proclaim:
01184 {
01185 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01186 ast->envs.env = tmpEnv;
01187
01188
01189 RESOLVE(ast->child(0), tmpEnv, lamLevel, DECL_MODE,
01190 idc_value, ast,
01191 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK)
01192 | RSLV_BIND_PUBLIC);
01193
01194
01195 RESOLVE(ast->child(1), tmpEnv, lamLevel, USE_MODE,
01196 idc_type, ast,
01197 flags | (RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01198
01199
01200 RESOLVE(ast->child(2), tmpEnv, lamLevel, USE_MODE,
01201 idc_type, ast,
01202 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01203
01204 env->mergeBindingsFrom(tmpEnv);
01205 break;
01206 }
01207
01208 case at_recdef:
01209 case at_define:
01210 {
01214
01215 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01216 ast->envs.env = tmpEnv;
01217
01218
01219
01220
01221
01222
01223
01224 assert(ast->child(0)->astType == at_identPattern);
01225 ast->child(0)->child(0)->flags &= ~ID_IS_MUTATED;
01226
01227 if (ast->astType == at_recdef) {
01228
01229
01230 RESOLVE(ast->child(0), tmpEnv, lamLevel, DEF_MODE,
01231 id_value, ast,
01232 flags | (RSLV_NEW_TV_OK) | RSLV_BIND_PUBLIC);
01233 }
01234
01235
01236 RESOLVE(ast->child(1), tmpEnv, lamLevel, USE_MODE,
01237 idc_value, ast,
01238 flags | (RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01239
01240 if (ast->astType == at_define) {
01241
01242
01243
01244 RESOLVE(ast->child(0), tmpEnv, lamLevel, DEF_MODE,
01245 id_value, ast,
01246 flags | (RSLV_NEW_TV_OK) | RSLV_BIND_PUBLIC);
01247 }
01248
01249
01250 RESOLVE(ast->child(2), tmpEnv, lamLevel, USE_MODE,
01251 idc_type, ast,
01252 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01253
01254
01255 ast->child(0)->child(0)->flags |= ID_MUT_CLOSED;
01256
01257 env->mergeBindingsFrom(tmpEnv);
01258 break;
01259 }
01260
01261 case at_deftypeclass:
01262 {
01263 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01264 ast->envs.env = tmpEnv;
01265
01266
01267 RESOLVE(ast->child(0), tmpEnv, lamLevel, DEF_MODE,
01268 id_typeclass, ast,
01269 (flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK)
01270 | RSLV_BIND_PUBLIC));
01271
01272
01273 RESOLVE(ast->child(1), tmpEnv, lamLevel, DEF_MODE,
01274 idc_type, ast,
01275 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01276
01277
01278
01279 RESOLVE(ast->child(2), tmpEnv, lamLevel, USE_MODE,
01280 idc_type, ast,
01281 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01282
01283
01284
01285
01286 RESOLVE(ast->child(4), tmpEnv, lamLevel, USE_MODE,
01287 idc_type, ast,
01288 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01289
01290
01291 RESOLVE(ast->child(5), tmpEnv, lamLevel, USE_MODE,
01292 idc_type, ast,
01293 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01294
01295 tmpEnv->setFlags(ast->child(0)->s, BF_COMPLETE);
01296 env->mergeBindingsFrom(tmpEnv);
01297 break;
01298 }
01299
01300 case at_tcdecls:
01301 {
01302
01303 for (size_t c = 0; c < ast->children.size(); c++)
01304 RESOLVE(ast->child(c), env, lamLevel,
01305 USE_MODE, identType, currLB, flags);
01306
01307 break;
01308 }
01309
01310 case at_tyfn:
01311 {
01312
01313 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01314 idc_type, currLB, flags);
01315
01316
01317 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01318 idc_type, currLB, flags);
01319 break;
01320 }
01321
01322 case at_tcapp:
01323 {
01324
01325 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01326 id_typeclass, currLB, flags);
01327
01328
01329 for (size_t c = 1; c < ast->children.size(); c++)
01330 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
01331 idc_type, currLB, flags);
01332
01333 break;
01334 }
01335
01336 case at_method_decls:
01337 {
01338
01339 for (size_t c = 0; c < ast->children.size(); c++)
01340 RESOLVE(ast->child(c), env, lamLevel,
01341 DEF_MODE, id_tcmethod, currLB, flags);
01342
01343 break;
01344 }
01345
01346 case at_method_decl:
01347 {
01348
01349 RESOLVE(ast->child(0), env, lamLevel, DEF_MODE,
01350 id_tcmethod, currLB,
01351 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK)
01352 | RSLV_BIND_PUBLIC);
01353
01354
01355 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01356 idc_type, currLB, flags);
01357
01358 break;
01359 }
01360
01361 case at_definstance:
01362 {
01363 shared_ptr<ASTEnvironment > tmpEnv = env->newDefScope();
01364 ast->envs.env = tmpEnv;
01365
01366
01367 RESOLVE(ast->child(0), tmpEnv, lamLevel,
01368 USE_MODE, idc_type, ast, flags | RSLV_NEW_TV_OK);
01369
01378
01379 if (errorFree) {
01380 shared_ptr<AST> tcName = ast->child(0)->child(0);
01381 shared_ptr<AST> tcSym = tcName->symbolDef;
01382 shared_ptr<AST> tcDef = tcSym->defForm;
01383 bool isClosed = (tcDef->child(3)->astType == at_oc_closed);
01384
01385 if (isClosed && tcSym->fqn.iface != env->uocName) {
01386 errStream << ast->loc
01387 << ": Type class " << tcSym->fqn
01388 << " is closed. Instances may not be defined by other"
01389 << " modules or interfaces."
01390 << std::endl;
01391 errorFree = false;
01392 }
01393 }
01394
01395
01396 RESOLVE(ast->child(1), tmpEnv, lamLevel,
01397 USE_MODE, idc_value, ast, flags);
01398
01399
01400 RESOLVE(ast->child(2), tmpEnv, lamLevel,
01401 USE_MODE, idc_type, ast, flags | RSLV_NEW_TV_OK);
01402 break;
01403 }
01404
01405 case at_tcmethods:
01406 {
01407
01408 for (size_t c = 0; c < ast->children.size(); c++)
01409 RESOLVE(ast->child(c), env, lamLevel,
01410 USE_MODE, idc_value, currLB, flags);
01411
01412
01413
01414
01415
01416
01417
01418 for (size_t i = 0; i < ast->children.size(); i++) {
01419 shared_ptr<AST> ith_bind = ast->child(i);
01420
01421 for (size_t j = i+1; j < ast->children.size(); j++) {
01422 shared_ptr<AST> jth_bind = ast->child(j);
01423
01424 if(ith_bind->child(0)->s == jth_bind->child(0)->s) {
01425 errStream << jth_bind->loc
01426 << ": Duplicate method definition for "
01427 << ith_bind->child(0)->s
01428 << std::endl;
01429 errorFree = false;
01430 break;
01431 }
01432 }
01433 }
01434
01435
01436
01437
01438
01439
01440
01441
01442 break;
01443 }
01444
01445 case at_tcmethod_binding:
01446 {
01447
01448 for (size_t c = 0; c < ast->children.size(); c++)
01449 RESOLVE(ast->child(c), env, lamLevel,
01450 USE_MODE, idc_value, currLB, flags);
01451 break;
01452 }
01453
01454 case at_ifident:
01455 break;
01456
01457 case at_provide:
01458 {
01459 shared_ptr<AST> ifAst = ast->child(0);
01460 if (!importIfBinding(errStream, aliasEnv, ifAst)) {
01461 errStream << ast->loc << ": "
01462 << "Unable to provide interface `"
01463 << ifAst->s
01464 << "' - interface file not found. "
01465 << std::endl;
01466 errorFree = false;
01467 break;
01468 }
01469
01470 shared_ptr<ASTEnvironment > ifEnv = ifAst->envs.env;
01471
01472 for (size_t i = 1; i < ast->children.size(); i++) {
01473 shared_ptr<AST> provideName=ast->child(i);
01474 shared_ptr<Binding<AST> > bndg = ifEnv->doGetBinding(provideName->s);
01475 if (!bndg) {
01476 errStream << ast->loc << ": "
01477 << provideName->s
01478 << " not found in interface "
01479 << ifAst->s
01480 << std::endl;
01481
01482 errorFree = false;
01483 break;
01484 }
01485
01486 if (!bndg->val->isDecl) {
01487 errStream << ast->loc << ": Cannot provide "
01488 << provideName->s
01489 << " in interface "
01490 << ifAst->s
01491 << ", which is defined in the interface."
01492 << std::endl;
01493
01494 errorFree = false;
01495 break;
01496 }
01497
01498 bndg->flags |= BF_PROVIDING;
01499 }
01500
01501 break;
01502 }
01503
01504 case at_importAs:
01505 {
01506 shared_ptr<AST> ifAst = ast->child(0);
01507 shared_ptr<AST> idAst = ast->child(1);
01508
01509 if (!importIfBinding(errStream, aliasEnv, ifAst)) {
01510 errStream << ast->loc << ": "
01511 << "Unable to import interface `"
01512 << ifAst->s
01513 << "' - interface file not found. "
01514 << std::endl;
01515 errorFree = false;
01516 break;
01517 }
01518
01519
01520 shared_ptr<ASTEnvironment > tmpEnv = env->newScope();
01521
01522
01523 ast->envs.env = tmpEnv;
01524
01525 if (ifAst->s == env->uocName) {
01526 errStream << ast->loc << ": "
01527 << "Cannot import an undefined interface. "
01528 << std::endl;
01529
01530 errorFree = false;
01531 break;
01532 }
01533
01534 RESOLVE(idAst, tmpEnv, lamLevel, DEF_MODE,
01535 id_interface, GC_NULL, flags);
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545 stringstream ifNameMap;
01546 ifNameMap << ":" << idAst->s << ":";
01547 std::string ifNameMapStr = ifNameMap.str();
01548
01549 aliasEnv->addBinding(ifNameMapStr, ifAst);
01550 aliasEnv->setFlags(ifNameMapStr, BF_PRIVATE);
01551
01552
01553 env->setFlags(idAst->s,
01554 ((env->getFlags(idAst->s)) |
01555 BF_PRIVATE));
01556
01557 idAst->envs.env = ifAst->envs.env;
01558 idAst->envs.gamma = ifAst->envs.gamma;
01559 idAst->envs.instEnv = ifAst->envs.instEnv;
01560
01561 aliasPublicBindings(idAst->s, GC_NULL, idAst->envs.env, tmpEnv);
01562 env->mergeBindingsFrom(tmpEnv);
01563 break;
01564 }
01565
01566 case at_import:
01567 {
01568
01569 shared_ptr<ASTEnvironment > tmpEnv = env->newScope();
01570 ast->envs.env = tmpEnv;
01571
01572 shared_ptr<AST> ifAst = ast->child(0);
01573
01574 if (ifAst->s == env->uocName) {
01575 errStream << ast->loc << ": "
01576 << "Cannot import an undefined interface. "
01577 << std::endl;
01578
01579 errorFree = false;
01580 break;
01581 }
01582
01583 if (!importIfBinding(errStream, aliasEnv, ifAst)) {
01584 errStream << ast->loc << ": "
01585 << "Unable to import interface `"
01586 << ifAst->s
01587 << "' - interface file not found. "
01588 << std::endl;
01589 errorFree = false;
01590 break;
01591 }
01592
01593 if (ast->children.size() == 1) {
01594
01595 aliasPublicBindings(std::string(), aliasEnv, ifAst->envs.env, tmpEnv);
01596 }
01597 else {
01598
01599 for (size_t c = 1; c < ast->children.size(); c++) {
01600 shared_ptr<AST> alias = ast->child(c);
01601 shared_ptr<AST> localName = alias->child(0);
01602 shared_ptr<AST> pubName = alias->child(1);
01603
01604 RESOLVE(pubName, ifAst->envs.env, lamLevel, USE_MODE,
01605 id_unresolved, currLB,
01606 ((flags & (~RSLV_NEW_TV_OK))) | RSLV_NO_CHK_USE_TYPE);
01607
01608 if (!errorFree)
01609 break;
01610
01611
01612 shared_ptr<Binding<AST> > bndg =
01613 ifAst->envs.env->doGetBinding(pubName->s);
01614
01615 std::string pubFQN = bndg->val->fqn.asString();
01616
01617 shared_ptr<AST> oldAlias = aliasEnv->getBinding(pubFQN);
01618 if (oldAlias) {
01619 errStream << alias->loc << ": The public identifier "
01620 << pubFQN
01621 << ", being aliased here to "
01622 << pubName->s
01623 << ", was previously aliased to "
01624 << oldAlias->s
01625 << " at "
01626 << oldAlias->loc
01627 << std::endl;
01628 errorFree = false;
01629 break;
01630 }
01631
01632 aliasEnv->addBinding(pubFQN, localName);
01633
01634 shared_ptr<AST> oldDef = env->getBinding(localName->s);
01635 if (oldDef) {
01636 errStream << alias->loc << ": Conflict for alias definition"
01637 << localName->s
01638 << ". Previously defined at "
01639 << oldDef->loc
01640 << std::endl;
01641 errorFree = false;
01642 break;
01643 }
01644
01645 tmpEnv->addBinding(localName->s, pubName->symbolDef);
01646 tmpEnv->setFlags(localName->s, BF_PRIVATE);
01647 }
01648 }
01649
01650 env->mergeBindingsFrom(tmpEnv);
01651 break;
01652 }
01653
01654 case at_ifsel:
01655 {
01656 assert(false);
01657 break;
01658 }
01659
01660 case at_declares:
01661 {
01662
01663 for (size_t c = 0; c < ast->children.size(); c++)
01664 RESOLVE(ast->child(c), env, lamLevel, NULL_MODE, identType,
01665 currLB, flags);
01666
01667 break;
01668 }
01669
01670 case at_declare:
01671 {
01672
01673
01674
01675
01676 std::string declID = ast->child(0)->s;
01677
01678 if (declID != "stateful"
01679 && declID != "tag") {
01680 errStream << ast->child(0)->loc
01681 << ": "
01682 << declID
01683 << " is not a recognized declare identifier"
01684 << std::endl;
01685 errorFree = false;
01686 }
01687
01688 if (declID == "stateful") {
01689 if (!(flags & RSLV_IS_INTERFACE)) {
01690 errStream << ast->child(0)->loc
01691 << ": Only Interfaces can be declared to be stateful"
01692 << std::endl;
01693 errorFree = false;
01694 }
01695 }
01696
01697 if (declID == "tag") {
01698 if (!(flags & RSLV_WITHIN_DEFUNION)) {
01699 errStream << ast->child(0)->loc
01700 << ": tag declaration can only occur within a defunion"
01701 << std::endl;
01702 errorFree = false;
01703 }
01704 }
01705
01706
01707 if (ast->children.size() > 1) {
01708 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01709 idc_type, currLB, flags);
01710 }
01711
01712 break;
01713 }
01714
01715 case at_tvlist:
01716 {
01717
01718 for (size_t c = 0; c < ast->children.size(); c++)
01719 RESOLVE(ast->child(c), env, lamLevel, mode, identType,
01720 currLB, flags);
01721
01722 break;
01723 }
01724
01725 case at_constructors:
01726
01727 {
01728 std::string ifName="";
01729 bool varConst = false;
01730
01731
01732
01733 if (ast->child(0)->child(0)->astType == at_usesel) {
01734 ifName = ast->child(0)->child(0)->child(0)->s;
01735 varConst = true;
01736 }
01737
01738
01739
01740 for (size_t c = 0; c < ast->children.size(); c++) {
01741 if (varConst) {
01742 if (ast->child(c)->child(0)->astType != at_usesel ||
01743 ast->child(c)->child(0)->child(0)->s != ifName) {
01744 errStream << ast->child(c)->child(0)->loc << ": "
01745 << " All Constructors of a Union declaration must "
01746 << "belong to the same interface "
01747 << std::endl;
01748 errorFree = false;
01749 }
01750 }
01751 else {
01752 if (ast->child(c)->child(0)->astType == at_usesel) {
01753 errStream << ast->child(c)->child(0)->loc << ": "
01754 << " All Constructors of a Union declaration must "
01755 << "belong to the same compilation Unit "
01756 << std::endl;
01757 errorFree = false;
01758 }
01759 }
01760
01761 RESOLVE(ast->child(c), env, lamLevel, NULL_MODE, identType,
01762 currLB, flags);
01763 }
01764 break;
01765 }
01766
01767 case at_constructor:
01768 {
01769
01770 IdentType it = ((ast->children.size() > 1) ?
01771 id_ucon : id_ucon0);
01772
01773 RESOLVE(ast->child(0), env, lamLevel, DEF_MODE,
01774 it, currLB, flags | RSLV_BIND_PUBLIC);
01775
01776 pair< set<string>::iterator, bool > pr;
01777 set<string> names;
01778
01779
01780
01781 for (size_t c = 1; c < ast->children.size(); c++) {
01782 shared_ptr<AST> fld = ast->child(c);
01783 RESOLVE(fld, env, lamLevel, USE_MODE, identType,
01784 currLB, flags);
01785
01786 if (fld->astType != at_field)
01787 continue;
01788
01789 pr = names.insert(fld->child(0)->s);
01790 if (!pr.second) {
01791 errStream << ast->loc << ": "
01792 << "Duplicate field label: "
01793 << fld->child(0)->s
01794 << std::endl;
01795 errorFree = false;
01796 }
01797 }
01798 break;
01799 }
01800
01801 case at_fields:
01802 {
01803
01804
01805
01806 pair< set<string>::iterator, bool > pr;
01807 set<string> names;
01808
01809 for (size_t c = 0; c < ast->children.size(); c++) {
01810 shared_ptr<AST> fld = ast->child(c);
01811 RESOLVE(fld, env, lamLevel, USE_MODE, identType,
01812 currLB, flags);
01813
01814 if (fld->astType == at_fill)
01815
01816 continue;
01817
01818 pr = names.insert(fld->child(0)->s);
01819 if (!pr.second) {
01820 errStream << ast->loc << ": "
01821 << "Duplicate field/method name: "
01822 << fld->child(0)->s
01823 << std::endl;
01824 errorFree = false;
01825 }
01826 }
01827 break;
01828 }
01829
01830 case at_field:
01831 case at_methdecl:
01832 {
01833
01834 ast->child(0)->fqn = FQName(FQ_NOIF, ast->child(0)->s);
01835
01836
01837 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01838 idc_type, currLB,
01839 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01840
01841 break;
01842 }
01843
01844 case at_fill:
01845 {
01846
01847 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01848 idc_type, currLB,
01849 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01850
01851 if(ast->children.size() == 2)
01852 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01853 idc_type, currLB,
01854 flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
01855
01856 break;
01857 }
01858
01859 case at_bitfieldType:
01860 {
01861
01862 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01863 idc_type, currLB, flags);
01864
01865
01866 RESOLVE(ast->child(1), env, lamLevel, NULL_MODE,
01867 identType, currLB,
01868 flags & (~RSLV_INCOMPLETE_OK));
01869 break;
01870 }
01871
01872 case at_arrayRefType:
01873 case at_byRefType:
01874 {
01875
01876 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01877 idc_type, currLB,
01878 flags | (RSLV_INCOMPLETE_OK));
01879 break;
01880 }
01881
01882 case at_boxedType:
01883 {
01884
01885 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01886 idc_type, currLB,
01887 flags | (RSLV_INCOMPLETE_OK));
01888 break;
01889 }
01890
01891 case at_unboxedType:
01892 {
01893
01894 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01895 idc_type, currLB,
01896 flags & (~RSLV_INCOMPLETE_OK));
01897
01898 break;
01899 }
01900
01901 case at_methType:
01902 case at_fn:
01903 {
01904
01905 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01906 idc_type, currLB, flags);
01907
01908
01909 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
01910 idc_type, currLB, flags);
01911
01912 break;
01913 }
01914
01915 case at_fnargVec:
01916 {
01917
01918 for (size_t c = 0; c < ast->children.size(); c++)
01919 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
01920 idc_type, currLB, flags);
01921
01922 break;
01923 }
01924
01925
01926 case at_primaryType:
01927 case at_fieldType:
01928 {
01929 break;
01930 }
01931
01932 case at_arrayType:
01933 {
01934
01935 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01936 idc_type, currLB, flags);
01937
01938
01939 RESOLVE(ast->child(1), env, lamLevel, NULL_MODE, identType,
01940 currLB, flags);
01941 if (ast->child(1)->litValue.i < 0) {
01942 errStream << ast->child(1)->loc << ": "
01943 << "Array index cannot be negative."
01944 << std::endl;
01945 errorFree = false;
01946 }
01947
01948 break;
01949 }
01950
01951 case at_vectorType:
01952 {
01953
01954 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01955 idc_type, currLB, flags);
01956
01957
01958 if (ast->children.size() > 1) {
01959 RESOLVE(ast->child(1), env, lamLevel, NULL_MODE, identType,
01960 currLB, flags);
01961 }
01962
01963 break;
01964 }
01965
01966 case at_exceptionType:
01967 {
01968 break;
01969 }
01970
01971 case at_dummyType:
01972 {
01973 break;
01974 }
01975
01976 case at_mutableType:
01977 case at_constType:
01978 {
01979
01980 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01981 idc_type, currLB, flags);
01982
01983 break;
01984 }
01985
01986 case at_typeapp:
01987 {
01988
01989 for (size_t c = 0; c < ast->children.size(); c++)
01990 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
01991 idc_type, currLB, flags);
01992
01993 break;
01994 }
01995
01996 case at_qualType:
01997 {
01998 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
01999 idc_type, currLB, flags);
02000 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02001 idc_type, currLB, flags);
02002 break;
02003 }
02004
02005 case at_constraints:
02006 {
02007 for (size_t c=0; c < ast->children.size(); c++)
02008 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02009 idc_type, currLB, flags);
02010 break;
02011 }
02012
02013 case at_identPattern:
02014 {
02015
02016 RESOLVE(ast->child(0), env, lamLevel, mode, identType,
02017 currLB, flags);
02018
02019
02020
02021 if (ast->children.size() > 1) {
02022 RESOLVE(ast->child(1), env, lamLevel, USE_MODE, idc_type,
02023 currLB, flags);
02024 }
02025
02026 break;
02027 }
02028
02029 case at_mixfix:
02030 {
02031
02032 for (size_t c = 0; c < ast->children.size(); c++)
02033 RESOLVE(ast->child(c), env, lamLevel, USE_MODE, idc_value,
02034 currLB, flags);
02035
02036
02037
02038 if (errorFree) {
02039 shared_ptr<AST> tree = ProcessMixFix(errStream, ast);
02040
02041 if (tree) {
02042
02043
02044
02045
02046
02047
02048
02049 if (tree->astType == at_apply)
02050 RESOLVE(tree, env, lamLevel, USE_MODE,
02051 idc_apply, currLB, flags);
02052
02053
02054
02055 REWRITE_AST_IN_PLACE(ast, tree);
02056 }
02057 else {
02058
02059 errorFree = false;
02060 }
02061 }
02062
02063 break;
02064 }
02065
02066 case at_typeAnnotation:
02067 {
02068
02069 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02070 idc_value, currLB, flags);
02071
02072 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02073 idc_type, currLB, flags);
02074
02075 break;
02076 }
02077
02078 case at_suspend:
02079 {
02080
02081 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02082 idc_value, currLB, flags);
02083
02084 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02085 idc_value, currLB, flags);
02086 break;
02087 }
02088
02089 case at_unit:
02090 {
02091 break;
02092 }
02093
02094 case at_allocREF:
02095 {
02096
02097 for (size_t c = 0; c < ast->children.size(); c++)
02098 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02099 idc_type, currLB, flags);
02100 break;
02101 }
02102
02103 case at_mkClosure:
02104 {
02105
02106 for (size_t c = 0; c < ast->children.size(); c++)
02107 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02108 idc_value, currLB,
02109 flags | RSLV_INCOMPLETE_OK);
02110 break;
02111 }
02112
02113 case at_mkArrayRef:
02114 {
02115
02116 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02117 idc_value, currLB, flags);
02118 break;
02119 }
02120
02121 case at_copyREF:
02122 case at_setClosure:
02123 {
02124
02125 for (size_t c = 0; c < ast->children.size(); c++)
02126 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02127 id_value, currLB, flags);
02128 break;
02129 }
02130
02131
02132 case at_MakeVector:
02133 {
02134
02135 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02136 idc_value, currLB, flags);
02137
02138
02139 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02140 idc_value, currLB, flags);
02141
02142 break;
02143 }
02144
02145 case at_array:
02146 case at_vector:
02147 {
02148
02149 for (size_t c = 0; c < ast->children.size(); c++)
02150 RESOLVE(ast->child(c), env, lamLevel, USE_MODE, idc_value,
02151 currLB, flags);
02152
02153 break;
02154 }
02155
02156 case at_begin:
02157 {
02158
02159 for (size_t c = 0; c < ast->children.size(); c++)
02160 RESOLVE(ast->child(c), env, lamLevel, USE_MODE, idc_value,
02161 currLB, flags);
02162
02163 break;
02164 }
02165
02166 case at_labeledBlock:
02167 {
02168 RESOLVE(ast->child(0), env, lamLevel, DEF_MODE,
02169 id_block, currLB, flags);
02170
02171
02172
02173 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02174 idc_value, currLB, flags);
02175 break;
02176 }
02177
02178 case at_return_from:
02179 {
02180 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02181 id_block, currLB, flags);
02182 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02183 idc_value, currLB, flags);
02184 break;
02185 }
02186
02187 case at_fqCtr:
02188 {
02189 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02190 idc_type, currLB, flags);
02191
02192
02193 break;
02194 }
02195
02196 case at_sel_ctr:
02197 {
02198 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02199 idc_value, currLB, flags );
02200
02201
02202 break;
02203 }
02204
02205 case at_select:
02206 {
02207
02208 shared_ptr<AST> lhs = ast->child(0);
02209
02210
02211 if (lhs->astType == at_ident || lhs->astType == at_usesel) {
02212
02213
02214
02215
02216
02217
02218
02219 RESOLVE(lhs, env, lamLevel, USE_MODE, identType, currLB,
02220 flags | RSLV_NO_CHK_USE_TYPE | RSLV_SWITCHED_ID_OK);
02221
02222
02223
02224 if (lhs->isIdentType(id_interface)) {
02225 ast->astType = at_usesel;
02226
02227
02228 RESOLVE(ast, env, lamLevel, USE_MODE, identType, currLB, flags);
02229 }
02230 else if (lhs->isIdentType(id_union)) {
02231
02232 ast->astType = at_fqCtr;
02233 RESOLVE(ast, env, lamLevel, USE_MODE, idc_type, currLB, flags);
02234 }
02235 else if (!lhs->isIdentType(id_value)) {
02236 errStream << lhs->loc
02237 << ": Identifier "
02238 << " `" << lhs->s << "'"
02239 << " is undefined." << std::endl;
02240 errorFree = false;
02241 break;
02242 }
02243 }
02244 else {
02245 RESOLVE(lhs, env, lamLevel, USE_MODE, idc_value, currLB, flags);
02246 }
02247
02248
02249
02250
02251
02252
02253 break;
02254 }
02255
02256 #ifdef HAVE_INDEXABLE_LENGTH_OPS
02257 case at_array_length:
02258 case at_array_ref_length:
02259 case at_vector_length:
02260 {
02261
02262 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02263 id_value, currLB, flags);
02264
02265 break;
02266 }
02267 #endif
02268
02269 case at_nth:
02270 case at_array_nth:
02271 case at_array_ref_nth:
02272 case at_vector_nth:
02273 {
02274
02275 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02276 id_value, currLB, flags);
02277
02278
02279 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02280 id_value, currLB, flags);
02281
02282 break;
02283 }
02284
02285 case at_lambda:
02286 {
02287 shared_ptr<ASTEnvironment > lamEnv = env->newScope();
02288 ast->envs.env = lamEnv;
02289
02290
02291 shared_ptr<AST> argVec = ast->child(0);
02292 for (size_t c = 0; c < argVec->children.size(); c++) {
02293 RESOLVE(argVec->child(c), lamEnv, lamEnv, DEF_MODE,
02294 id_value, currLB, flags);
02295 shared_ptr<AST> id = argVec->child(c)->child(0);
02296 lamEnv->setFlags(id->s, BF_COMPLETE);
02297 }
02298
02299
02300 RESOLVE(ast->child(1), lamEnv, lamEnv, USE_MODE,
02301 idc_value, currLB,
02302 flags | (RSLV_INCOMPLETE_OK));
02303 break;
02304 }
02305
02306 case at_argVec:
02307 {
02308 assert(false);
02309 break;
02310 }
02311
02312 case at_struct_apply:
02313 case at_object_apply:
02314 case at_ucon_apply:
02315 case at_apply:
02316 {
02317 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02318 idc_apply, currLB, flags);
02319
02320 for (size_t c = 1; c < ast->children.size(); c++)
02321 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02322 idc_value, currLB, flags);
02323
02324
02325 if (ast->child(0)->s == "and") {
02326 shared_ptr<AST> node =
02327 AST::make(at_and, ast->loc, ast->child(1), ast->child(2));
02328
02329 REWRITE_AST_IN_PLACE(ast, node);
02330 }
02331 else if (ast->child(0)->s == "or") {
02332 shared_ptr<AST> node =
02333 AST::make(at_and, ast->loc, ast->child(1), ast->child(2));
02334
02335 REWRITE_AST_IN_PLACE(ast, node);
02336 }
02337
02338 break;
02339 }
02340
02341 case at_if:
02342 {
02343
02344 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02345 idc_value, currLB, flags);
02346
02347
02348 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02349 idc_value, currLB, flags);
02350
02351
02352 RESOLVE(ast->child(2), env, lamLevel, USE_MODE,
02353 idc_value, currLB, flags);
02354
02355 break;
02356 }
02357
02358 case at_when:
02359 case at_unless:
02360 case at_and:
02361 case at_or:
02362 {
02363
02364 for (size_t c = 0; c < ast->children.size(); c++)
02365 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02366 idc_value, currLB, flags);
02367
02368 break;
02369 }
02370
02371 case at_cond:
02372 {
02373
02374 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02375 idc_value, currLB, flags);
02376
02377
02378 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02379 idc_value, currLB, flags);
02380 break;
02381 }
02382
02383 case at_cond_legs:
02384 {
02385
02386 for (size_t c = 0; c < ast->children.size(); c++)
02387 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02388 idc_value, currLB, flags);
02389
02390 break;
02391 }
02392
02393 case at_cond_leg:
02394 {
02395
02396 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02397 idc_value, currLB, flags);
02398
02399
02400 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02401 idc_value, currLB, flags);
02402
02403 break;
02404 }
02405
02406 case at_condelse:
02407 {
02408
02409 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02410 idc_value, currLB, flags);
02411 break;
02412 }
02413
02414 case at_setbang:
02415 {
02416 shared_ptr<AST> lhs = ast->child(0);
02417 shared_ptr<AST> rhs = ast->child(1);
02418
02419
02420 RESOLVE(lhs, env, lamLevel, USE_MODE, id_value, currLB, flags);
02421
02422
02423 RESOLVE(rhs, env, lamLevel, USE_MODE, idc_value, currLB, flags);
02424
02425 if (lhs->astType == at_ident)
02426 if ((lhs->symbolDef->flags & ID_MUT_CLOSED) == 0)
02427 lhs->symbolDef->flags |= ID_IS_MUTATED;
02428
02429 break;
02430 }
02431
02432 case at_sizeof:
02433 case at_bitsizeof:
02434 {
02435
02436
02437
02438
02439 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02440 idc_type, currLB,
02441 flags | (~RSLV_INCOMPLETE_OK));
02442 break;
02443 }
02444
02445 case at_dup:
02446 {
02447
02448 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02449 idc_value, currLB, flags);
02450 break;
02451 }
02452
02453 case at_deref:
02454 {
02455
02456 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02457 id_value, currLB, flags);
02458
02459 break;
02460 }
02461
02462 case at_inner_ref:
02463 {
02464
02465 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02466 idc_value, currLB, flags);
02467
02468 if (((ast->flags & INNER_REF_NDX) == 0) &&
02469 ast->child(1)->astType == at_ident) {
02470
02471 }
02472 else {
02473
02474 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02475 id_value, currLB, flags);
02476 }
02477 break;
02478 }
02479
02480 case at_uswitch:
02481 {
02482
02483
02484
02485 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02486 idc_value, currLB, flags);
02487
02488
02489 RESOLVE(ast->child(2), env, lamLevel, USE_MODE,
02490 idc_value, currLB, flags);
02491
02492
02493 if (ast->child(3)->astType != at_Null)
02494 RESOLVE(ast->child(3), env, lamLevel, USE_MODE,
02495 idc_value, currLB, flags);
02496
02497 break;
02498 }
02499
02500 case at_usw_legs:
02501 {
02502
02503 for (size_t c = 0; c < ast->children.size(); c++)
02504 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02505 idc_value, currLB, flags);
02506
02507 break;
02508 }
02509
02510 case at_usw_leg:
02511 case at_otherwise:
02512 {
02513 shared_ptr<ASTEnvironment > legEnv = env->newScope();
02514 ast->envs.env = legEnv;
02515
02516
02517 RESOLVE(ast->child(0), legEnv, lamLevel, DEF_MODE,
02518 id_value, currLB, flags);
02519 ASTEnvironment::iterator itr = legEnv->find(ast->child(0)->s);
02520 assert(itr != legEnv->end());
02521 itr->second->flags |= BF_COMPLETE;
02522
02523 if (ast->astType == at_usw_leg)
02524 ast->child(0)->flags |= ID_FOR_USWITCH;
02525
02526
02527 if ((flags & RSLV_WITHIN_CATCH) && (ast->children.size() > 3))
02528 flags |= RSLV_WITHIN_CATCH_MC;
02529
02530 RESOLVE(ast->child(1), legEnv, lamLevel, USE_MODE,
02531 idc_value, currLB, flags);
02532
02533
02534 for (size_t c=2; c < ast->children.size(); c++)
02535 RESOLVE(ast->child(c), legEnv, lamLevel, USE_MODE,
02536 idc_uctor, currLB, flags);
02537
02538 break;
02539 }
02540
02541 case at_try:
02542 {
02543
02544 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02545 id_value, currLB, flags);
02546
02547
02548
02549
02550 RESOLVE(ast->child(2), env, lamLevel, USE_MODE,
02551 idc_value, currLB, flags | RSLV_WITHIN_CATCH);
02552
02553
02554 if (ast->child(3)->astType != at_Null)
02555 RESOLVE(ast->child(3), env, lamLevel, USE_MODE,
02556 idc_value, currLB, flags | RSLV_WITHIN_CATCH);
02557 break;
02558 }
02559
02560 case at_throw:
02561 {
02562 ResolverFlags rf = flags;
02563
02564 if (ast->child(0)->astType == at_ident)
02565 rf |= RSLV_SWITCHED_ID_OK;
02566
02567
02568 RESOLVE(ast->child(0), env, lamLevel, USE_MODE,
02569 idc_value, currLB,
02570 rf);
02571 break;
02572 }
02573
02574 case at_container:
02575 {
02576 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02577 idc_value, currLB,
02578 flags);
02579 break;
02580 }
02581
02582 case at_loop:
02583 {
02584
02585 shared_ptr<ASTEnvironment > loopEnv = env->newScope();
02586 ast->envs.env = loopEnv;
02587
02588 shared_ptr<AST> lbs = ast->child(0);
02589
02590
02591
02592 for (size_t c = 0; c < lbs->children.size(); c++) {
02593 shared_ptr<AST> lb = lbs->child(c);
02594 shared_ptr<AST> init = lb->child(1);
02595 RESOLVE(init, loopEnv, lamLevel, USE_MODE, idc_value,
02596 currLB, flags);
02597 }
02598
02599
02600 for (size_t c = 0; c < lbs->children.size(); c++) {
02601 shared_ptr<AST> lb = lbs->child(c);
02602 shared_ptr<AST> localDef = lb->child(0);
02603
02604 RESOLVE(localDef, loopEnv, lamLevel, DEF_MODE,
02605 id_value, currLB, flags);
02606 }
02607
02608
02609 markComplete(loopEnv);
02610
02611
02612 for (size_t c = 0; c < lbs->children.size(); c++) {
02613 shared_ptr<AST> lb = lbs->child(c);
02614 shared_ptr<AST> step = lb->child(2);
02615 RESOLVE(step, loopEnv, lamLevel, USE_MODE,
02616 idc_value, currLB, flags);
02617 }
02618
02619
02620
02621 RESOLVE(ast->child(1), loopEnv, lamLevel, USE_MODE,
02622 idc_value, currLB, flags);
02623
02624
02625
02626 RESOLVE(ast->child(2), loopEnv, lamLevel, USE_MODE,
02627 idc_value, currLB, flags);
02628 break;
02629 }
02630
02631 case at_looptest:
02632 {
02633 for (size_t c = 0; c < ast->children.size(); c++)
02634 RESOLVE(ast->child(c), env, lamLevel, USE_MODE,
02635 idc_value, currLB, flags);
02636 break;
02637 }
02638
02639 case at_let:
02640 {
02641
02642
02643
02644
02645
02646
02647 shared_ptr<ASTEnvironment > letEnv = env->newScope();
02648 shared_ptr<AST> lbs = ast->child(0);
02649 lbs->parentLB = currLB;
02650
02651 ast->envs.env = letEnv;
02652 lbs->envs.env = letEnv;
02653
02654
02655 lbs->flags &= ~LBS_PROCESSED;
02656
02657
02658
02659
02660 for (size_t c = 0; c < lbs->children.size(); c++) {
02661 shared_ptr<AST> lb = lbs->child(c);
02662
02663 RESOLVE(lb->child(1), letEnv, lamLevel, USE_MODE,
02664 idc_value, lbs, flags);
02665 }
02666
02667
02668
02669 for (size_t c = 0; c < lbs->children.size(); c++) {
02670 shared_ptr<AST> lb = lbs->child(c);
02671
02672
02673 RESOLVE(lb->child(0), letEnv, lamLevel, DEF_MODE,
02674 id_value, lbs, flags);
02675
02676 assert(lb->child(0)->astType == at_identPattern);
02677 lb->child(0)->child(0)->flags &= ~ID_IS_MUTATED;
02678 }
02679
02680
02681 lbs->flags |= LBS_PROCESSED;
02682
02683
02684
02685 markComplete(letEnv);
02686 RESOLVE(ast->child(1), letEnv, lamLevel, USE_MODE,
02687 idc_value, currLB, flags);
02688
02689
02690 RESOLVE(ast->child(2), letEnv, lamLevel, USE_MODE,
02691 idc_type, currLB, flags & (~RSLV_NEW_TV_OK) & (~RSLV_INCOMPLETE_OK));
02692 break;
02693 }
02694
02695 case at_letStar:
02696 {
02697
02698
02699
02700 shared_ptr<ASTEnvironment > letEnv = env->newScope();
02701 shared_ptr<AST> lbs = ast->child(0);
02702 lbs->parentLB = currLB;
02703
02704 ast->envs.env = letEnv;
02705 lbs->envs.env = letEnv;
02706
02707
02708
02709 lbs->flags &= ~LBS_PROCESSED;
02710
02711
02712
02713
02714
02715 for (size_t c = 0; c < lbs->children.size(); c++) {
02716 shared_ptr<AST> lb = lbs->child(c);
02717
02718 RESOLVE(lb->child(1), letEnv, lamLevel, USE_MODE,
02719 idc_value, lbs, flags);
02720
02721 RESOLVE(lb->child(0), letEnv, lamLevel, DEF_MODE,
02722 id_value, lbs, flags);
02723 assert(lb->child(0)->astType == at_identPattern);
02724 lb->child(0)->child(0)->flags &= ~ID_IS_MUTATED;
02725
02726 ASTEnvironment::iterator itr =
02727 letEnv->find(lb->child(0)->child(0)->s);
02728 itr->second->flags |= BF_COMPLETE;
02729 }
02730
02731
02732 lbs->flags |= LBS_PROCESSED;
02733
02734
02735 RESOLVE(ast->child(1), letEnv, lamLevel, USE_MODE,
02736 idc_value, currLB, flags);
02737
02738 break;
02739 }
02740
02741 case at_letrec:
02742 {
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752 shared_ptr<ASTEnvironment > letEnv = env->newScope();
02753 shared_ptr<AST> lbs = ast->child(0);
02754 lbs->parentLB = currLB;
02755
02756 ast->envs.env = letEnv;
02757 lbs->envs.env = letEnv;
02758
02759
02760 lbs->flags &= ~LBS_PROCESSED;
02761
02762
02763 for (size_t c = 0; c < lbs->children.size(); c++) {
02764 shared_ptr<AST> lb = lbs->child(c);
02765
02766
02767 RESOLVE(lb->child(0), letEnv, lamLevel, DEF_MODE,
02768 id_value, lbs, flags);
02769 assert(lb->child(0)->astType == at_identPattern);
02770 lb->child(0)->child(0)->flags &= ~ID_IS_MUTATED;
02771 }
02772
02773
02774 for (size_t c = 0; c < lbs->children.size(); c++) {
02775 shared_ptr<AST> lb = lbs->child(c);
02776
02777
02778 RESOLVE(lb->child(1), letEnv, lamLevel, USE_MODE,
02779 idc_value, lbs, flags);
02780
02781 }
02782
02783
02784 lbs->flags |= LBS_PROCESSED;
02785
02786
02787
02788
02789
02790 markComplete(letEnv);
02791 RESOLVE(ast->child(1), letEnv, lamLevel, USE_MODE,
02792 idc_value, currLB, flags);
02793
02794 break;
02795 }
02796
02797
02798
02799
02800
02801
02802
02803
02804
02805
02806 case at_letbinding:
02807 {
02808
02809
02810 if (ast->flags & LB_REC_BIND) {
02811 RESOLVE(ast->child(0), env, lamLevel, DEF_MODE,
02812 id_value, ast, flags);
02813
02814 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02815 idc_value, ast, flags);
02816 }
02817 else {
02818 RESOLVE(ast->child(1), env, lamLevel, USE_MODE,
02819 idc_value, ast, flags);
02820
02821 RESOLVE(ast->child(0), env, lamLevel, DEF_MODE,
02822 idc_value, ast, flags);
02823 }
02824
02825 assert(ast->child(0)->astType == at_identPattern);
02826 ast->child(0)->child(0)->flags &= ~ID_IS_MUTATED;
02827 break;
02828 }
02829 }
02830 return errorFree;
02831 }
02832
02833 static bool
02834 initEnv(std::ostream& errStream,
02835 shared_ptr<AST> ast,
02836 shared_ptr<ASTEnvironment > aliasEnv,
02837 shared_ptr<ASTEnvironment > env)
02838 {
02839
02840 if (ast->astType == at_interface &&
02841 ast->child(0)->s == "bitc.prelude") {
02842
02843
02844 return true;
02845 }
02846
02847
02848
02849 shared_ptr<ASTEnvironment > preenv = GC_NULL;
02850 UocMap::iterator itr = UocInfo::ifList.find("bitc.prelude");
02851 if (itr == UocInfo::ifList.end()) {
02852 errStream << ast->loc << ": "
02853 << "Internal Compiler Error. "
02854 << " Prelude has NOT been processed."
02855 << std::endl;
02856 ::exit(1);
02857 }
02858 preenv = itr->second->env;
02859
02860 if (!preenv) {
02861
02862
02863 errStream << ast->loc << ": "
02864 << "Internal Compiler Error. "
02865 << " Prelude's environment is NULL "
02866 << std::endl;
02867 ::exit(1);
02868 }
02869
02870 aliasPublicBindings(std::string(), aliasEnv, preenv, env);
02871 return true;
02872 }
02873
02874
02875 bool
02876 UocInfo::DoResolve(std::ostream& errStream, bool init,
02877 ResolverFlags rflags)
02878 {
02879 bool errFree = true;
02880
02881 if (Options::noPrelude)
02882 rflags |= RSLV_SYM_NO_PRELUDE;
02883
02884 shared_ptr<ASTEnvironment > aliasEnv = ASTEnvironment::make("*aliases*");
02885
02886 if (init) {
02887 if (false) {
02888 assert(env);
02889 assert(env->parent);
02890 env = env->parent->newDefScope();
02891 }
02892 else {
02893 env = ASTEnvironment::make(uocName);
02894 }
02895
02896 if ((rflags & RSLV_SYM_NO_PRELUDE) == 0)
02897 initEnv(std::cerr, uocAst, aliasEnv, env);
02898 }
02899
02900 CHKERR(errFree, resolve(errStream, uocAst, aliasEnv, env, GC_NULL,
02901 USE_MODE, idc_type, GC_NULL, rflags));
02902
02903 return errFree;
02904 }
02905
02906 bool
02907 UocInfo::Resolve(std::ostream& errStream, bool init,
02908 ResolverFlags rflags, std::string mesg)
02909 {
02910 bool errFree = true;
02911 errFree = DoResolve(errStream, init, rflags);
02912 if (!errFree)
02913 errStream << mesg
02914 << std::endl;
02915 return errFree;
02916 }
02917
02918 bool
02919 UocInfo::fe_symresolve(std::ostream& errStream,
02920 bool init, unsigned long flags)
02921 {
02922 return DoResolve(errStream, init, RSLV_NO_FLAGS);
02923 }
02924
02925