Пример #1
0
        public SourceCompilerResults compile(SourceCompilerParameters parameters, IProgressMonitor monitor)
        {
            this.parameters = parameters;
            if (parameters.FullBuild)
            {
                return(compileCore(monitor));
            }

            try {
                monitor.beginTask("", 5);

                var results = compileCore(new SubProgressMonitor(monitor, 2));
                if (results.Failed)
                {
                    if (results.MissingType == null)
                    {
                        Environment.trace(this, "Errors: all dependent types rebuild required");
                        // Try to build with all the dependencies
                        parameters.ProgressiveBuild = false;
                        results = compileCore(new SubProgressMonitor(monitor, 3));
                    }
                    else
                    {
                        Environment.trace(this, "Missing type '" + results.MissingType + "': full rebuild");
                        parameters.FullBuild = true;
                        results = compileCore(new SubProgressMonitor(monitor, 3));
                    }
                }
                else
                {
                    // Check for type structural modifications
                    var oldDependencyInfo = parameters.DependencyInfo;
                    var newDependencyInfo = results.DependencyInfo;
                    var oldTypes          = results.CompiledFiles.selectMany(p => oldDependencyInfo.getFileContents(p)).toList();
                    var newTypes          = results.CompiledFiles.selectMany(p => newDependencyInfo.getFileContents(p)).toList();
                    var typesToRebuild    = Query.empty <String>();
                    foreach (var type in oldTypes.except(newTypes))
                    {
                        typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
                    }
                    foreach (var type in oldTypes.intersect(newTypes))
                    {
                        if (!JvmTypeSystemHelper.isStructurallyEqual(JvmTypeSystemHelper.getType(parameters.TypeSystem, type),
                                                                     JvmTypeSystemHelper.getType(results.TypeSystem, type)))
                        {
                            Environment.trace(this, "Structurally changed: " + type);
                            typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
                        }
                    }
                    var filesToRebuild = typesToRebuild.selectMany(p => oldDependencyInfo.getTypeLocations(p));
                    filesToRebuild = filesToRebuild.distinct().except(results.CompiledFiles).toList();
                    monitor.worked(1);

                    if (filesToRebuild.any())
                    {
                        Environment.trace(this, "Structural changes: referencing types rebuild required (" + filesToRebuild.count() + " files)");

                        parameters.ProgressiveBuild = false;
                        results = compileCore(new SubProgressMonitor(monitor, 3));

                        /* The compilation units are not using the current type system
                         *
                         * // If some types have changed, rebuild the referencing types
                         * var oldResult = results;
                         * this.parameters = new SourceCompilerParameters();
                         * foreach (var f in filesToRebuild) {
                         *      this.parameters.getFilesToCompile().add(f);
                         * }
                         * this.parameters.AllFiles.addAll(parameters.AllFiles.getAllResources());
                         * this.parameters.DependencyInfo = newDependencyInfo;
                         * this.parameters.ClassPath = parameters.ClassPath;
                         * this.parameters.TypeSystem = results.TypeSystem;
                         *
                         * results = compileCore(new SubProgressMonitor(monitor, 2));
                         *
                         * foreach (var e in oldResult.CompilationUnits.entrySet()) {
                         *      results.CompilationUnits[e.Key] = e.Value;
                         * }
                         * foreach (var e in oldResult.ClassFiles.entrySet()) {
                         *      results.ClassFiles[e.Key] = e.Value;
                         * }
                         * results.CompiledFiles.addAll(oldResult.CompiledFiles);
                         */
                    }
                    else
                    {
                        monitor.worked(2);
                    }
                }
                return(results);
            } finally {
                monitor.done();
            }
        }
Пример #2
0
        private SourceCompilerResults compileCore(IProgressMonitor monitor)
        {
            var t0 = System.nanoTime();

            var results      = new SourceCompilerResults();
            var hasErrors    = false;
            var errorManager = new CodeErrorManager();
            var allFiles     = parameters.AllFiles;
            Iterable <IFile> filesToCompile = null;

            try {
                monitor.beginTask("", 11);

                var deletedFiles = parameters.FilesToCompile
                                   .select(p => allFiles.getResource(p))
                                   .where (p => p == null || !p.exists())
                                   .select(p => allFiles.getProjectRelativeName(p)).toSet();
                var typesToCopy = Query.empty <TypeInfo>();

                // Get the files to compile
                if (parameters.FullBuild)
                {
                    filesToCompile = allFiles.getAllResources().where (p => p.exists()).toList();
                }
                else
                {
                    bool filteringDone    = false;
                    var  referencingFiles = parameters.getDependencyInfo().getAllReferencingFiles(parameters.getFilesToCompile());
                    if (parameters.ProgressiveBuild && deletedFiles.isEmpty())
                    {
                        var referencedFiles = parameters.getDependencyInfo().getAllReferencedFiles(parameters.getFilesToCompile());
                        referencedFiles = referencedFiles.except(parameters.getFilesToCompile());
                        referencedFiles = referencedFiles.intersect(referencingFiles);

                        // Progressive build only if referenced and referencing files do not intersect
                        if (!referencedFiles.any())
                        {
                            filesToCompile = parameters.FilesToCompile.select(p => allFiles.getResource(p)).where (p => p.exists()).toList();
                            filteringDone  = true;
                        }
                    }
                    if (!filteringDone)
                    {
                        // Incremental build with dependencies
                        filesToCompile = referencingFiles.select(p => allFiles.getResource(p)).where (p => p.exists()).toList();
                    }

                    var filesToKeep = allFiles.getAllProjectRelativeNames().except(referencingFiles);
                    typesToCopy = filesToKeep.selectMany(p => parameters.DependencyInfo.getFileContents(p))
                                  .where (p => p.indexOf('$') == -1).select(p => parameters.TypeSystem.getType(p));

                    Environment.trace(this, "keeping " + filesToKeep.count() + " files");
                    Environment.trace(this, "ignoring " +
                                      (allFiles.getAllResources().count() - filesToCompile.count() - filesToKeep.count()) + " files");
                }
                Environment.trace(this, "compiling " + filesToCompile.count() + " files");
                monitor.worked(1);
                if (monitor.isCanceled())
                {
                    throw new InterruptedException();
                }

                var compilationUnits = new HashMap <String, CompilationUnitNode>();

                // Parsing
                foreach (var file in filesToCompile)
                {
                    var text = getText(file);
                    if (text != null)
                    {
                        parse(file, text, errorManager, compilationUnits);
                    }
                }
                monitor.worked(1);
                if (monitor.isCanceled())
                {
                    throw new InterruptedException();
                }

                // Compiling
                var t1 = System.nanoTime();

                var typeSystem = new Library(parameters.ClassPath);
                JvmTypeSystemHelper.cloneTypes(typesToCopy, typeSystem);

                var annotatedTypeSystem = new Library(new[] { Environment.getLibraryPath("stabal.jar") }, typeSystem);

                var cparams = new CompilerParameters();
                cparams.TypeSystem          = typeSystem;
                cparams.AnnotatedTypeSystem = annotatedTypeSystem;
                cparams.GenerateClassFiles  = parameters.GenerateClassFiles;
                cparams.ProgressTracker     = new CompilationProgressTracker(monitor);

                var cunits   = compilationUnits.values().toArray(new CompilationUnitNode[compilationUnits.size()]);
                var cresults = new StabCompiler().compileFromCompilationUnits(cparams, cunits);

                Environment.trace(this, "compilation of " + sizeof(cunits) + " files done in " + ((System.nanoTime() - t1) / 1e6) + "ms");

                foreach (var error in cresults.Errors)
                {
                    if (error.Level == 0)
                    {
                        hasErrors = true;
                    }
                    results.CodeErrors.add(error);
                    Environment.trace(this, "error (" + error.Line + ", " + error.Column + ") " + error.Filename + ": " + error.Message);
                }

                if (!hasErrors)
                {
                    var dependencyInfo = new DependencyInfo();
                    results.DependencyInfo = dependencyInfo;
                    var allTypes = new HashSet <String>();

                    // Copy informations from unbuilt files
                    if (parameters.DependencyInfo != null)
                    {
                        var unbuiltFiles = allFiles.getAllProjectRelativeNames();
                        unbuiltFiles = unbuiltFiles.except(filesToCompile.select(p => allFiles.getProjectRelativeName(p)));
                        unbuiltFiles = unbuiltFiles.except(deletedFiles);
                        foreach (var file in unbuiltFiles)
                        {
                            foreach (var type in parameters.DependencyInfo.getFileContents(file))
                            {
                                allTypes.add(type);
                                dependencyInfo.addFileToTypeRelation(file, type);
                                foreach (var refType in parameters.DependencyInfo.getReferencedTypes(type))
                                {
                                    dependencyInfo.addTypeToTypeRelation(type, refType);
                                }
                            }
                        }
                    }

                    // Collect the types and update the dependencies.
                    var typeMembers = new HashMap <IFile, Iterable <TypeMemberNode> >();
                    foreach (var file in filesToCompile)
                    {
                        var fileName        = allFiles.getProjectRelativeName(file);
                        var compilationUnit = compilationUnits[fileName];
                        if (compilationUnit == null)
                        {
                            continue;
                        }
                        var members = SyntaxTreeHelper.getTypeMembers(compilationUnit);
                        typeMembers.put(file, members);
                        foreach (var member in members)
                        {
                            var typeName = member.getUserData(typeof(TypeInfo)).FullName;
                            dependencyInfo.addFileToTypeRelation(fileName, typeName);
                            allTypes.add(typeName);
                        }
                    }

                    if (parameters.DependencyInfo != null)
                    {
                        // Copy the types ignored by this compilation
                        var missingTypes = new HashSet <TypeInfo>();
                        foreach (var t in allTypes.where (p => p.indexOf('$') == -1 && !typeSystem.typeExists(p)))
                        {
                            if (hasErrors = !parameters.DependencyInfo.getReferencedTypes(t).all(p => allTypes.contains(p)))
                            {
                                Environment.trace(this, "Incremental build failed: a type was deleted");
                                break;
                            }
                            missingTypes.add(parameters.TypeSystem.getType(t));
                        }
                        if (!hasErrors)
                        {
                            JvmTypeSystemHelper.cloneTypes(missingTypes, typeSystem);
                        }
                    }

                    if (!hasErrors)
                    {
                        // Compute the dependencies in the compiled files
                        foreach (var member in filesToCompile.select(p => typeMembers[p]).where (p => p != null).selectMany(p => p))
                        {
                            foreach (var t in SyntaxTreeHelper.getTypeMemberDependencies(member)
                                     .intersect(allTypes.select(p => JvmTypeSystemHelper.getType(typeSystem, p))))
                            {
                                dependencyInfo.addTypeToTypeRelation(member.getUserData(typeof(TypeInfo)).FullName, t.FullName);
                            }
                        }

                        results.TypeSystem          = typeSystem;
                        results.AnnotatedTypeSystem = annotatedTypeSystem;
                        foreach (var e in compilationUnits.entrySet())
                        {
                            results.CompilationUnits[e.Key] = e.Value;
                        }
                        foreach (var e in cresults.ClassFiles.entrySet())
                        {
                            results.ClassFiles[e.Key] = e.Value;
                        }
                    }
                }
                monitor.worked(1);
            } catch (CodeErrorException e) {
                monitor.worked(10);
            } catch (TypeLoadException e) {
                results.MissingType = e.TypeName;
                hasErrors           = true;
                monitor.worked(6);
            } finally {
                monitor.done();
            }

            foreach (var file in filesToCompile)
            {
                results.CompiledFiles.add(allFiles.getProjectRelativeName(file));
            }
            foreach (var error in errorManager.Errors)
            {
                if (error.Level == 0)
                {
                    hasErrors = true;
                }
                results.CodeErrors.add(error);
                Environment.trace(this, "error (" + error.Line + ", " + error.Column + ") " + error.Filename + ": " + error.Message);
            }
            results.Failed = hasErrors;

            Environment.trace(this, "compilation done in " + ((System.nanoTime() - t0) / 1e6) + "ms");
            return(results);
        }
        private IProject[] buildDelta(IResourceDelta delta, IProgressMonitor monitor)
        {
            Environment.trace(this, "Incremental build");
            var t0 = System.nanoTime();

            try {
                monitor.beginTask("", 5);

                var filesToCompile = EclipseHelper.getModifiedFiles(delta, Query.singleton("stab"), Query.singleton("bin"));
                var deletedFiles   = filesToCompile.where (p => !p.exists()).select(p => p.getProjectRelativePath().toPortableString());
                Environment.trace(this, filesToCompile.count() + " files to compile");

                var parameters = new SourceCompilerParameters {
                    ProgressiveBuild = true,
                    ClassPath        = projectManager.ClassPath,
                    TypeSystem       = projectManager.TypeSystem,
                    DependencyInfo   = projectManager.DependencyInfo
                };
                parameters.AllFiles.addAll(projectManager.getSourceFiles());
                parameters.AllFiles.addAll(filesToCompile);
                foreach (var f in filesToCompile)
                {
                    parameters.FilesToCompile.add(parameters.AllFiles.getProjectRelativeName(f));
                }
                foreach (var f in projectManager.FilesToCompile)
                {
                    parameters.FilesToCompile.add(f);
                }
                foreach (var s in projectManager.Properties.PreprocessorSymbols)
                {
                    parameters.PreprocessorSymbols.add(s);
                }

                var compiler = new SourceCompiler();
                var results  = compiler.compile(parameters, new SubProgressMonitor(monitor, 3));
                if (results.Failed)
                {
                    if (results.MissingType != null)
                    {
                        projectManager.DependencyInfo = null;
                        IMarker marker = getProject().getFile(".stabproperties").createMarker(IMarker.PROBLEM);
                        marker.setAttribute(IMarker.MESSAGE, results.MissingType);
                        marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                        marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
                    }
                    else
                    {
                        projectManager.FilesToCompile = projectManager.FilesToCompile.union(results.CompiledFiles).toList();
                    }
                }
                else
                {
                    // Update the .class files
                    var outputFolder = getProject().getFolder(projectManager.Properties.OutputPath);
                    foreach (var typeName in results.CompiledFiles.selectMany(p => results.DependencyInfo.getFileContents(p)))
                    {
                        var file = outputFolder.getFile(typeName + ".class");
                        if (!file.exists())
                        {
                            continue;
                        }
                        var type = JvmTypeSystemHelper.getType(results.TypeSystem, typeName);
                        var declaringTypeName = type.Name;
                        var declaringType     = type;
                        while ((declaringType = declaringType.DeclaringType) != null)
                        {
                            declaringTypeName = declaringType.Name + "$" + declaringTypeName;
                        }
                        // Deletes all the nested types of the declaring type
                        file.getParent().accept(p => {
                            if (p.getType() == IResource.FILE)
                            {
                                if (p.getName().startsWith(declaringTypeName + "$"))
                                {
                                    p.requestResource().delete(IResource.FORCE, null);
                                }
                                return(false);
                            }
                            return(true);
                        }, IResource.NONE);
                        file.delete(IResource.FORCE, null);
                    }
                    foreach (var e in results.ClassFiles.entrySet())
                    {
                        var fileName = e.Key.replace('.', '/') + ".class";
                        var file     = outputFolder.getFile(fileName);
                        EclipseHelper.createFolders(file);
                        file.create(new ByteArrayInputStream(e.Value), IResource.FORCE, null);
                    }

                    // TODO: delete the .class generated from the types contained in deletedFiles

                    projectManager.DependencyInfo      = results.DependencyInfo;
                    projectManager.TypeSystem          = results.TypeSystem;
                    projectManager.AnnotatedTypeSystem = results.AnnotatedTypeSystem;
                    projectManager.FilesToCompile      = Query.empty();

                    var compilationUnits = new HashMap <String, CompilationUnitNode>(projectManager.CompilationUnits);
                    foreach (var e in results.CompilationUnits.entrySet())
                    {
                        compilationUnits[e.Key] = e.Value;
                    }
                    foreach (var s in deletedFiles)
                    {
                        compilationUnits.remove(s);
                    }
                    projectManager.CompilationUnits = compilationUnits;

                    Environment.fireProjectBuildEvent(projectManager);
                }
                updateMarkers(results.CodeErrors, parameters.AllFiles);

                monitor.worked(1);
            } catch (InterruptedException) {
            } finally {
                monitor.done();
            }

            Environment.trace(this, "Incremental build done in " + ((System.nanoTime() - t0) / 1e6) + "ms");
            return(null);
        }