private void loadCompiledModules(
     List<JCCompilationUnit> trees, LinkedList<JCCompilationUnit> moduleTrees) {
   compilerDelegate.visitModules(phasedUnits);
   Modules modules = ceylonContext.getModules();
   // now make sure the phase units have their modules and packages set correctly
   for (PhasedUnit pu : phasedUnits.getPhasedUnits()) {
     Package pkg = pu.getPackage();
     loadModuleFromSource(pkg, modules, moduleTrees, trees);
   }
   // also make sure we have packages and modules set up for every Java file we compile
   for (JCCompilationUnit cu : trees) {
     // skip Ceylon CUs
     if (cu instanceof CeylonCompilationUnit) continue;
     String packageName = "";
     if (cu.pid != null) packageName = TreeInfo.fullName(cu.pid).toString();
     /*
      * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
      */
     Package pkg = modelLoader.findOrCreateModulelessPackage(packageName);
     loadModuleFromSource(pkg, modules, moduleTrees, trees);
   }
   for (PhasedUnit phasedUnit : phasedUnits.getPhasedUnits()) {
     for (Tree.ModuleDescriptor modDescr :
         phasedUnit.getCompilationUnit().getModuleDescriptors()) {
       String name = phasedUnit.getPackage().getNameAsString();
       CeylonPhasedUnit cpu = (CeylonPhasedUnit) phasedUnit;
       CeylonFileObject cfo = (CeylonFileObject) cpu.getFileObject();
       moduleNamesToFileObjects.put(name, cfo);
     }
   }
 }
  private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
      throw new RuntimeException(
          "Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
      String source = readSource.toString();
      ANTLRStringStream input = new ANTLRStringStream(source);
      CeylonLexer lexer = new CeylonLexer(input);

      CommonTokenStream tokens = new CommonTokenStream(lexer);

      CeylonParser parser = new CeylonParser(tokens);
      CompilationUnit cu = parser.compilationUnit();

      char[] chars = source.toCharArray();
      LineMap map = Position.makeLineMap(chars, chars.length, false);

      java.util.List<LexError> lexerErrors = lexer.getErrors();
      for (LexError le : lexerErrors) {
        printError(le, le.getMessage(), "ceylon.lexer", map);
      }

      java.util.List<ParseError> parserErrors = parser.getErrors();
      for (ParseError pe : parserErrors) {
        printError(pe, pe.getMessage(), "ceylon.parser", map);
      }

      if (lexer.getNumberOfSyntaxErrors() != 0) {
        log.error("ceylon.lexer.failed");
      } else if (parser.getNumberOfSyntaxErrors() != 0) {
        log.error("ceylon.parser.failed");
      } else {
        ModuleManager moduleManager = phasedUnits.getModuleManager();
        File sourceFile = new File(filename.toString());
        // FIXME: temporary solution
        VirtualFile file = vfs.getFromFile(sourceFile);
        VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));
        // FIXME: this is bad in many ways
        String pkgName = getPackage(filename);
        // make a Package with no module yet, we will resolve them later
        com.redhat.ceylon.compiler.typechecker.model.Package p =
            modelLoader.findOrCreatePackage(null, pkgName == null ? "" : pkgName);
        PhasedUnit phasedUnit =
            new CeylonPhasedUnit(file, srcDir, cu, p, moduleManager, ceylonContext, filename, map);
        phasedUnits.addPhasedUnit(file, phasedUnit);
        gen.setMap(map);

        return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
      }
    } catch (Exception e) {
      log.error("ceylon", e.getMessage());
    }

    JCCompilationUnit result =
        make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
  }
 private void loadCompiledModules(
     List<JCCompilationUnit> trees, LinkedList<JCCompilationUnit> moduleTrees) {
   phasedUnits.visitModules();
   Modules modules = ceylonContext.getModules();
   // now make sure the phase units have their modules and packages set correctly
   for (PhasedUnit pu : phasedUnits.getPhasedUnits()) {
     Package pkg = pu.getPackage();
     loadModuleFromSource(pkg, modules, moduleTrees);
   }
   // also make sure we have packages and modules set up for every Java file we compile
   for (JCCompilationUnit cu : trees) {
     // skip Ceylon CUs
     if (cu instanceof CeylonCompilationUnit) continue;
     String packageName = "";
     if (cu.pid != null) packageName = TreeInfo.fullName(cu.pid).toString();
     Package pkg = modelLoader.findOrCreatePackage(null, packageName);
     loadModuleFromSource(pkg, modules, moduleTrees);
   }
 }
示例#4
0
 @Override
 public void initRound(JavaCompiler prev) {
   super.initRound(prev);
   // round compilers don't add module trees, it's already done by the first one
   addModuleTrees = false;
   PhasedUnits oldPUs = ((LanguageCompiler) prev).phasedUnits;
   ModuleManager moduleManager = phasedUnits.getModuleManager();
   ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
   for (PhasedUnit pu : oldPUs.getPhasedUnits()) {
     if (pu instanceof CeylonPhasedUnit) {
       CeylonPhasedUnit cpu = (CeylonPhasedUnit) pu;
       // FIXME: this is bad in many ways
       String pkgName;
       try {
         pkgName = getPackage(((CeylonPhasedUnit) pu).getFileObject());
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         continue;
       }
       // make a Package with no module yet, we will resolve them later
       /*
        * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
        */
       com.redhat.ceylon.model.typechecker.model.Package p =
           modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
       CeylonPhasedUnit newPu =
           new CeylonPhasedUnit(
               pu.getUnitFile(),
               pu.getSrcDir(),
               pu.getCompilationUnit(),
               p,
               moduleManager,
               moduleSourceMapper,
               ceylonContext,
               cpu.getFileObject(),
               cpu.getLineMap());
       phasedUnits.addPhasedUnit(pu.getUnitFile(), newPu);
     } else {
       phasedUnits.addPhasedUnit(pu.getUnitFile(), pu);
     }
   }
 }
示例#5
0
 private Module loadModuleFromSource(
     String pkgName,
     LinkedList<JCCompilationUnit> moduleTrees,
     List<JCCompilationUnit> parsedTrees) {
   if (pkgName.isEmpty()) return null;
   String moduleClassName = pkgName + ".module";
   JavaFileObject fileObject;
   try {
     if (options.get(Option.VERBOSE) != null) {
       log.printRawLines(
           WriterKind.NOTICE, "[Trying to load source for module " + moduleClassName + "]");
     }
     fileObject =
         fileManager.getJavaFileForInput(
             StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
     if (options.get(Option.VERBOSE) != null) {
       log.printRawLines(WriterKind.NOTICE, "[Got file object: " + fileObject + "]");
     }
   } catch (IOException e) {
     e.printStackTrace();
     return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
   }
   if (fileObject != null) {
     // first make sure we're not already compiling it: this can happen if we have several versions
     // of the
     // same module already loaded: we will get one which isn't the one we compile, but that's not
     // the one
     // we really want to compile.
     for (JCCompilationUnit parsedTree : parsedTrees) {
       if (parsedTree.sourcefile.equals(fileObject)
           && parsedTree instanceof CeylonCompilationUnit) {
         // same file! we already parsed it, let's return this one's module
         PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
         // the module visitor does load the module but does not set the unit's package module
         if (phasedUnit.getPackage().getModule() == null) {
           // so find the module it created
           for (Module mod : ceylonContext.getModules().getListOfModules()) {
             // we recognise it with the unit
             if (mod.getUnit() == phasedUnit.getUnit()) {
               // set the package's module
               Package pkg = phasedUnit.getPackage();
               pkg.setModule(mod);
               mod.getPackages().add(pkg);
               modulesLoadedFromSource.add(mod);
               break;
             }
           }
         }
         // now return it
         return phasedUnit.getPackage().getModule();
       }
     }
     JCCompilationUnit javaCompilationUnit = parse(fileObject);
     Module module;
     if (javaCompilationUnit instanceof CeylonCompilationUnit) {
       CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
       moduleTrees.add(ceylonCompilationUnit);
       // parse the module info from there
       module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
       ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
       // now set the module
       if (module != null) {
         ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
       }
     } else {
       // there was a syntax error in the module descriptor, make a pretend module so that we can
       // correctly mark all declarations as part of that module, but we won't generate any code
       // for it
       ModuleManager moduleManager = phasedUnits.getModuleManager();
       module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
     }
     // now remember it
     if (module != null) {
       modulesLoadedFromSource.add(module);
       return module;
     }
   }
   return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
 }
示例#6
0
  private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
      throw new RunTwiceException(
          "Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
      ModuleManager moduleManager = phasedUnits.getModuleManager();
      ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
      File sourceFile = new File(filename.getName());
      // FIXME: temporary solution
      VirtualFile file = vfs.getFromFile(sourceFile);
      VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));

      String source = readSource.toString();
      char[] chars = source.toCharArray();
      LineMap map = Position.makeLineMap(chars, chars.length, false);

      PhasedUnit phasedUnit = null;

      PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);

      String suppressWarnings = options.get(Option.CEYLONSUPPRESSWARNINGS);
      final EnumSet<Warning> suppressedWarnings;
      if (suppressWarnings != null) {
        if (suppressWarnings.trim().isEmpty()) {
          suppressedWarnings = EnumSet.allOf(Warning.class);
        } else {
          suppressedWarnings = EnumSet.noneOf(Warning.class);
          for (String name : suppressWarnings.trim().split(" *, *")) {
            suppressedWarnings.add(Warning.valueOf(name));
          }
        }
      } else {
        suppressedWarnings = EnumSet.noneOf(Warning.class);
      }

      if (externalPhasedUnit != null) {
        phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
        phasedUnit.setSuppressedWarnings(suppressedWarnings);
        phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
        gen.setMap(map);

        String pkgName = phasedUnit.getPackage().getQualifiedNameString();
        if ("".equals(pkgName)) {
          pkgName = null;
        }
        return gen.makeJCCompilationUnitPlaceholder(
            phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
      }
      if (phasedUnit == null) {
        ANTLRStringStream input = new NewlineFixingStringStream(source);
        CeylonLexer lexer = new CeylonLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        CeylonParser parser = new CeylonParser(tokens);
        CompilationUnit cu = parser.compilationUnit();

        java.util.List<LexError> lexerErrors = lexer.getErrors();
        for (LexError le : lexerErrors) {
          printError(le, le.getMessage(), "ceylon.lexer", map);
        }

        java.util.List<ParseError> parserErrors = parser.getErrors();
        for (ParseError pe : parserErrors) {
          printError(pe, pe.getMessage(), "ceylon.parser", map);
        }

        // if we continue and it's not a descriptor, we don't care about errors
        if ((options.get(Option.CEYLONCONTINUE) != null
                && !ModuleManager.MODULE_FILE.equals(sourceFile.getName())
                && !ModuleManager.PACKAGE_FILE.equals(sourceFile.getName()))
            // otherwise we care about errors
            || (lexerErrors.size() == 0 && parserErrors.size() == 0)) {
          // FIXME: this is bad in many ways
          String pkgName = getPackage(filename);
          // make a Package with no module yet, we will resolve them later
          /*
           * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
           */
          com.redhat.ceylon.model.typechecker.model.Package p =
              modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
          phasedUnit =
              new CeylonPhasedUnit(
                  file,
                  srcDir,
                  cu,
                  p,
                  moduleManager,
                  moduleSourceMapper,
                  ceylonContext,
                  filename,
                  map);
          phasedUnit.setSuppressedWarnings(suppressedWarnings);
          phasedUnits.addPhasedUnit(file, phasedUnit);
          gen.setMap(map);

          return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
        }
      }
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    JCCompilationUnit result =
        make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
  }
  private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
      throw new RuntimeException(
          "Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
      ModuleManager moduleManager = phasedUnits.getModuleManager();
      File sourceFile = new File(filename.getName());
      // FIXME: temporary solution
      VirtualFile file = vfs.getFromFile(sourceFile);
      VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));

      String source = readSource.toString();
      char[] chars = source.toCharArray();
      LineMap map = Position.makeLineMap(chars, chars.length, false);

      PhasedUnit phasedUnit = null;

      PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);

      if (externalPhasedUnit != null) {
        phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
        phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
        gen.setMap(map);

        String pkgName = phasedUnit.getPackage().getQualifiedNameString();
        if ("".equals(pkgName)) {
          pkgName = null;
        }
        return gen.makeJCCompilationUnitPlaceholder(
            phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
      }
      if (phasedUnit == null) {
        ANTLRStringStream input = new ANTLRStringStream(source);
        CeylonLexer lexer = new CeylonLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        CeylonParser parser = new CeylonParser(tokens);
        CompilationUnit cu = parser.compilationUnit();

        java.util.List<LexError> lexerErrors = lexer.getErrors();
        for (LexError le : lexerErrors) {
          printError(le, le.getMessage(), "ceylon.lexer", map);
        }

        java.util.List<ParseError> parserErrors = parser.getErrors();
        for (ParseError pe : parserErrors) {
          printError(pe, pe.getMessage(), "ceylon.parser", map);
        }

        if (lexerErrors.size() == 0 && parserErrors.size() == 0) {
          // FIXME: this is bad in many ways
          String pkgName = getPackage(filename);
          // make a Package with no module yet, we will resolve them later
          /*
           * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
           */
          com.redhat.ceylon.compiler.typechecker.model.Package p =
              modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
          phasedUnit =
              new CeylonPhasedUnit(
                  file, srcDir, cu, p, moduleManager, ceylonContext, filename, map);
          phasedUnits.addPhasedUnit(file, phasedUnit);
          gen.setMap(map);

          return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
        }
      }
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    JCCompilationUnit result =
        make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
  }
 @Override
 public void visitModules(PhasedUnits phasedUnits) {
   phasedUnits.visitModules();
 }