Add support for mutually recursive imports.
authorDavid Anderson <dvander@alliedmods.net>
Sat Jan 12 16:35:26 2013 -0800 (2013-01-12)
changeset 26005de34f712fe
parent 259 99ab8b19f0b9
child 261 219b49bf4a03
Add support for mutually recursive imports.
src/compiler/CompileContext.cpp
src/compiler/NameBinding.cpp
tests/basic/recursive_import_1_a.out
tests/basic/recursive_import_1_a.sp
tests/basic/recursive_import_1_b.sp
     1.1 --- a/src/compiler/CompileContext.cpp	Sat Jan 12 16:26:09 2013 -0800
     1.2 +++ b/src/compiler/CompileContext.cpp	Sat Jan 12 16:35:26 2013 -0800
     1.3 @@ -231,7 +231,7 @@
     1.4  bool
     1.5  CompileContext::add(Handle<Module> module)
     1.6  {
     1.7 -    if (module->compiled())
     1.8 +    if (module->compiled() || module->translationUnit())
     1.9          return true;
    1.10  
    1.11      TranslationUnit *tu = new (zone_->pool()) TranslationUnit(module);
    1.12 @@ -239,6 +239,8 @@
    1.13      if (!unparsed_.append(tu) || !units_.append(tu))
    1.14          return false;
    1.15  
    1.16 +    module->setTranslationUnit(tu);
    1.17 +
    1.18      return true;
    1.19  }
    1.20  
    1.21 @@ -289,9 +291,6 @@
    1.22          if (unit->failed())
    1.23              continue;
    1.24  
    1.25 -        // Mark the module as currently in translation.
    1.26 -        unit->module()->setTranslationUnit(unit);
    1.27 -
    1.28          PopulateNamesAndTypes(zone_, *this, unit);
    1.29      }
    1.30  
    1.31 @@ -309,11 +308,6 @@
    1.32          BindNamesAndTypes(zone_, *this, unit);
    1.33      }
    1.34  
    1.35 -    // Intermediate phase. Decide which modules we should not perform
    1.36 -    // semantic analysis on.
    1.37 -    if (!propagateFailure())
    1.38 -        return false;
    1.39 -
    1.40      // Phase 4. Now we can finally perform SemA+bytecode compilation.
    1.41      for (size_t i = 0; i < units_.length(); i++) {
    1.42          TranslationUnit *unit = units_[i];
     2.1 --- a/src/compiler/NameBinding.cpp	Sat Jan 12 16:26:09 2013 -0800
     2.2 +++ b/src/compiler/NameBinding.cpp	Sat Jan 12 16:35:26 2013 -0800
     2.3 @@ -897,7 +897,9 @@
     2.4                  assert(false);
     2.5              } else {
     2.6                  Local<Module> module(zone_, Module::cast(importable));
     2.7 -                if (!module->globals())
     2.8 +
     2.9 +                // Always grab from the translation unit if we can, for now.
    2.10 +                if (module->translationUnit())
    2.11                      importFromTranslationUnit(node, module->translationUnit());
    2.12                  else
    2.13                      assert(false);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/basic/recursive_import_1_a.out	Sat Jan 12 16:35:26 2013 -0800
     3.3 @@ -0,0 +1,1 @@
     3.4 +10
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/basic/recursive_import_1_a.sp	Sat Jan 12 16:35:26 2013 -0800
     4.3 @@ -0,0 +1,9 @@
     4.4 +from recursive_import_1_b import f;
     4.5 +
     4.6 +native PrintNum(num);
     4.7 +
     4.8 +public main()
     4.9 +{
    4.10 +    f();
    4.11 +}
    4.12 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tests/basic/recursive_import_1_b.sp	Sat Jan 12 16:35:26 2013 -0800
     5.3 @@ -0,0 +1,6 @@
     5.4 +from recursive_import_1_a import PrintNum;
     5.5 +
     5.6 +public f()
     5.7 +{
     5.8 +    PrintNum(10);
     5.9 +}