1 /*******************************************************************************
 
   2  * Copyright (c) 2000, 2003 IBM Corporation and others.
 
   3  * All rights reserved. This program and the accompanying materials 
 
   4  * are made available under the terms of the Common Public License v1.0
 
   5  * which accompanies this distribution, and is available at
 
   6  * http://www.eclipse.org/legal/cpl-v10.html
 
   9  *     IBM Corporation - initial API and implementation
 
  10  *******************************************************************************/
 
  11 package net.sourceforge.phpdt.internal.compiler;
 
  14  * A compilation result consists of all information returned by the compiler for 
 
  15  * a single compiled compilation source unit.  This includes:
 
  17  * <li> the compilation unit that was compiled
 
  18  * <li> for each type produced by compiling the compilation unit, its binary and optionally its principal structure
 
  19  * <li> any problems (errors or warnings) produced
 
  20  * <li> dependency info
 
  23  * The principle structure and binary may be null if the compiler could not produce them.
 
  24  * If neither could be produced, there is no corresponding entry for the type.
 
  26  * The dependency info includes type references such as supertypes, field types, method
 
  27  * parameter and return types, local variable types, types of intermediate expressions, etc.
 
  28  * It also includes the namespaces (packages) in which names were looked up.
 
  29  * It does <em>not</em> include finer grained dependencies such as information about
 
  30  * specific fields and methods which were referenced, but does contain their 
 
  31  * declaring types and any other types used to locate such fields or methods.
 
  36 //import java.util.Enumeration;
 
  37 import java.util.Hashtable;
 
  40 import net.sourceforge.phpdt.core.compiler.IProblem;
 
  41 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
 
  42 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
 
  44 public class CompilationResult {
 
  46         public IProblem problems[];
 
  47         public IProblem tasks[];
 
  48         public int problemCount;
 
  50         public ICompilationUnit compilationUnit;
 
  51         private Map problemsMap;
 
  52         private Map firstErrorsMap;
 
  53         private int maxProblemPerUnit;
 
  54         public char[][][] qualifiedReferences;
 
  55         public char[][] simpleNameReferences;
 
  57         public int lineSeparatorPositions[];
 
  58 //      public Hashtable compiledTypes = new Hashtable(11);
 
  59         public int unitIndex, totalUnitsKnown;
 
  60         public boolean hasBeenAccepted = false;
 
  61         public char[] fileName;
 
  63         public CompilationResult(
 
  67                 int maxProblemPerUnit){
 
  69                 this.fileName = fileName;
 
  70                 this.unitIndex = unitIndex;
 
  71                 this.totalUnitsKnown = totalUnitsKnown;
 
  72                 this.maxProblemPerUnit = maxProblemPerUnit;
 
  75         public CompilationResult(
 
  76                 ICompilationUnit compilationUnit,
 
  79                 int maxProblemPerUnit){
 
  81                 this.fileName = compilationUnit.getFileName();
 
  82                 this.compilationUnit = compilationUnit;
 
  83                 this.unitIndex = unitIndex;
 
  84                 this.totalUnitsKnown = totalUnitsKnown;
 
  85                 this.maxProblemPerUnit = maxProblemPerUnit;
 
  88         private int computePriority(IProblem problem){
 
  90                 final int P_STATIC = 1000;
 
  91                 final int P_OUTSIDE_METHOD = 4000;
 
  92                 final int P_FIRST_ERROR = 2000;
 
  93                 final int P_ERROR = 10000;
 
  95                 int priority = 1000 - problem.getSourceLineNumber(); // early problems first
 
  96                 if (priority < 0) priority = 0;
 
  97                 if (problem.isError()){
 
 100                 ReferenceContext context = problemsMap == null ? null : (ReferenceContext) problemsMap.get(problem);
 
 101                 if (context != null){
 
 102 //                      if (context instanceof AbstractMethodDeclaration){
 
 103 //                              AbstractMethodDeclaration method = (AbstractMethodDeclaration) context;
 
 104 //                              if (method.isStatic()) {
 
 105 //                                      priority += P_STATIC;
 
 108                         priority += P_OUTSIDE_METHOD;
 
 111                         priority += P_OUTSIDE_METHOD;
 
 113                 if (firstErrorsMap.containsKey(problem)){
 
 114                         priority += P_FIRST_ERROR;
 
 120         public IProblem[] getAllProblems() {
 
 121                 IProblem[] problems = this.getProblems();
 
 122                 int problemCount = problems != null ? problems.length : 0;
 
 123                 IProblem[] tasks = this.getTasks();
 
 124                 int taskCount = tasks != null ? tasks.length : 0;
 
 125                 if (taskCount == 0) {
 
 128                 if (problemCount == 0) {
 
 132                 int totalNumberOfProblem = problemCount + taskCount;
 
 133                 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
 
 134                 int allProblemIndex = 0;
 
 136                 int problemIndex = 0;
 
 137                 while (taskIndex + problemIndex < totalNumberOfProblem) {
 
 138                         IProblem nextTask = null;
 
 139                         IProblem nextProblem = null;
 
 140                         if (taskIndex < taskCount) {
 
 141                                 nextTask = tasks[taskIndex];
 
 143                         if (problemIndex < problemCount) {
 
 144                                 nextProblem = problems[problemIndex];
 
 146                         // select the next problem
 
 147                         IProblem currentProblem = null;
 
 148                         if (nextProblem != null) {
 
 149                                 if (nextTask != null) {
 
 150                                         if (nextProblem.getSourceStart() < nextTask.getSourceStart()) {
 
 151                                                 currentProblem = nextProblem;
 
 154                                                 currentProblem = nextTask;
 
 158                                         currentProblem = nextProblem;
 
 162                                 if (nextTask != null) {
 
 163                                         currentProblem = nextTask;
 
 167                         allProblems[allProblemIndex++] = currentProblem;
 
 172 //      public ClassFile[] getClassFiles() {
 
 173 //              Enumeration enum = compiledTypes.elements();
 
 174 //              ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
 
 176 //              while (enum.hasMoreElements()){
 
 177 //                      classFiles[index++] = (ClassFile)enum.nextElement();
 
 179 //              return classFiles;      
 
 183          * Answer the initial compilation unit corresponding to the present compilation result
 
 185         public ICompilationUnit getCompilationUnit(){
 
 186                 return compilationUnit;
 
 190          * Answer the initial file name
 
 192         public char[] getFileName(){
 
 197          * Answer the errors encountered during compilation.
 
 199         public IProblem[] getErrors() {
 
 201                 IProblem[] problems = getProblems();
 
 203                 for (int i = 0; i < this.problemCount; i++) {
 
 204                         if (problems[i].isError()) errorCount++;
 
 206                 if (errorCount == this.problemCount) return problems;
 
 207                 IProblem[] errors = new IProblem[errorCount];
 
 209                 for (int i = 0; i < this.problemCount; i++) {
 
 210                         if (problems[i].isError()) errors[index++] = problems[i];
 
 216          * Answer the problems (errors and warnings) encountered during compilation.
 
 218          * This is not a compiler internal API - it has side-effects !
 
 219          * It is intended to be used only once all problems have been detected,
 
 220          * and makes sure the problems slot as the exact size of the number of
 
 223         public IProblem[] getProblems() {
 
 225                 // Re-adjust the size of the problems if necessary.
 
 226                 if (problems != null) {
 
 228                         if (this.problemCount != problems.length) {
 
 229                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
 
 232                         if (this.maxProblemPerUnit > 0 && this.problemCount > this.maxProblemPerUnit){
 
 233                                 quickPrioritize(problems, 0, problemCount - 1);
 
 234                                 this.problemCount = this.maxProblemPerUnit;
 
 235                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
 
 238                         // Sort problems per source positions.
 
 239                         quickSort(problems, 0, problems.length-1);
 
 245          * Answer the tasks (TO-DO, ...) encountered during compilation.
 
 247          * This is not a compiler internal API - it has side-effects !
 
 248          * It is intended to be used only once all problems have been detected,
 
 249          * and makes sure the problems slot as the exact size of the number of
 
 252         public IProblem[] getTasks() {
 
 254                 // Re-adjust the size of the tasks if necessary.
 
 255                 if (this.tasks != null) {
 
 257                         if (this.taskCount != this.tasks.length) {
 
 258                                 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
 
 260                         quickSort(tasks, 0, tasks.length-1);
 
 265         public boolean hasErrors() {
 
 267                 if (problems != null)
 
 268                         for (int i = 0; i < problemCount; i++) {
 
 269                                 if (problems[i].isError())
 
 275         public boolean hasProblems() {
 
 277                 return problemCount != 0;
 
 280         public boolean hasSyntaxError(){
 
 282                 if (problems != null)
 
 283                         for (int i = 0; i < problemCount; i++) {
 
 284                                 IProblem problem = problems[i];
 
 285                                 if ((problem.getID() & IProblem.Syntax) != 0 && problem.isError())
 
 291         public boolean hasTasks() {
 
 292                 return this.taskCount != 0;
 
 295         public boolean hasWarnings() {
 
 297                 if (problems != null)
 
 298                         for (int i = 0; i < problemCount; i++) {
 
 299                                 if (problems[i].isWarning())
 
 305         private static void quickSort(IProblem[] list, int left, int right) {
 
 307                 if (left >= right) return;
 
 309                 // sort the problems by their source start position... starting with 0
 
 310                 int original_left = left;
 
 311                 int original_right = right;
 
 312                 int mid = list[(left + right) / 2].getSourceStart();
 
 314                         while (list[left].getSourceStart() < mid)
 
 316                         while (mid < list[right].getSourceStart())
 
 319                                 IProblem tmp = list[left];
 
 320                                 list[left] = list[right];
 
 325                 } while (left <= right);
 
 326                 if (original_left < right)
 
 327                         quickSort(list, original_left, right);
 
 328                 if (left < original_right)
 
 329                         quickSort(list, left, original_right);
 
 332         private void quickPrioritize(IProblem[] list, int left, int right) {
 
 334                 if (left >= right) return;
 
 336                 // sort the problems by their priority... starting with the highest priority
 
 337                 int original_left = left;
 
 338                 int original_right = right;
 
 339                 int mid = computePriority(list[(left + right) / 2]);
 
 341                         while (computePriority(list[right]) < mid)
 
 343                         while (mid < computePriority(list[left]))
 
 346                                 IProblem tmp = list[left];
 
 347                                 list[left] = list[right];
 
 352                 } while (left <= right);
 
 353                 if (original_left < right)
 
 354                         quickPrioritize(list, original_left, right);
 
 355                 if (left < original_right)
 
 356                         quickPrioritize(list, left, original_right);
 
 360          * For now, remember the compiled type using its compound name.
 
 362 //      public void record(char[] typeName, ClassFile classFile) {
 
 364 //              compiledTypes.put(typeName, classFile);
 
 367         public void record(IProblem newProblem, ReferenceContext referenceContext) {
 
 369                 if (newProblem.getID() == IProblem.Task) {
 
 370                         recordTask(newProblem);
 
 373                 if (problemCount == 0) {
 
 374                         problems = new IProblem[5];
 
 375                 } else if (problemCount == problems.length) {
 
 376                         System.arraycopy(problems, 0, (problems = new IProblem[problemCount * 2]), 0, problemCount);
 
 378                 problems[problemCount++] = newProblem;
 
 379                 if (referenceContext != null){
 
 380                         if (problemsMap == null) problemsMap = new Hashtable(5);
 
 381                         if (firstErrorsMap == null) firstErrorsMap = new Hashtable(5);
 
 382                         if (newProblem.isError() && !referenceContext.hasErrors()) firstErrorsMap.put(newProblem, newProblem);
 
 383                         problemsMap.put(newProblem, referenceContext);
 
 387         private void recordTask(IProblem newProblem) {
 
 388                 if (this.taskCount == 0) {
 
 389                         this.tasks = new IProblem[5];
 
 390                 } else if (this.taskCount == this.tasks.length) {
 
 391                         System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount * 2]), 0, this.taskCount);
 
 393                 this.tasks[this.taskCount++] = newProblem;
 
 396         public CompilationResult tagAsAccepted(){
 
 398                 this.hasBeenAccepted = true;
 
 399                 this.problemsMap = null; // flush
 
 403         public String toString(){
 
 405                 StringBuffer buffer = new StringBuffer();
 
 406                 if (this.fileName != null){
 
 407                         buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
 
 409 //              if (this.compiledTypes != null){
 
 410 //                      buffer.append("COMPILED type(s) \n");  //$NON-NLS-1$
 
 411 //                      Enumeration typeNames = this.compiledTypes.keys();
 
 412 //                      while (typeNames.hasMoreElements()) {
 
 413 //                              char[] typeName = (char[]) typeNames.nextElement();
 
 414 //                              buffer.append("\t - ").append(typeName).append('\n');   //$NON-NLS-1$
 
 418 //                      buffer.append("No COMPILED type\n");  //$NON-NLS-1$
 
 420                 if (problems != null){
 
 421                         buffer.append(this.problemCount).append(" PROBLEM(s) detected \n"); //$NON-NLS-1$//$NON-NLS-2$
 
 422                         for (int i = 0; i < this.problemCount; i++){
 
 423                                 buffer.append("\t - ").append(this.problems[i]).append('\n'); //$NON-NLS-1$
 
 426                         buffer.append("No PROBLEM\n"); //$NON-NLS-1$
 
 428                 return buffer.toString();