synchronized from quantum plugin
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / CompilationResult.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler;
12
13 /**
14  * A compilation result consists of all information returned by the compiler for 
15  * a single compiled compilation source unit.  This includes:
16  * <ul>
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
21  * </ul>
22  *
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.
25  *
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.
32  */
33
34
35
36 //import java.util.Enumeration;
37 import java.util.Hashtable;
38 import java.util.Map;
39
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;
43
44 public class CompilationResult {
45         
46         public IProblem problems[];
47         public IProblem tasks[];
48         public int problemCount;
49         public int taskCount;
50         public ICompilationUnit compilationUnit;
51         private Map problemsMap;
52         private Map firstErrorsMap;
53         private int maxProblemPerUnit;
54         public char[][][] qualifiedReferences;
55         public char[][] simpleNameReferences;
56
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;
62         
63         public CompilationResult(
64                 char[] fileName,
65                 int unitIndex, 
66                 int totalUnitsKnown,
67                 int maxProblemPerUnit){
68         
69                 this.fileName = fileName;
70                 this.unitIndex = unitIndex;
71                 this.totalUnitsKnown = totalUnitsKnown;
72                 this.maxProblemPerUnit = maxProblemPerUnit;
73         }
74         
75         public CompilationResult(
76                 ICompilationUnit compilationUnit,
77                 int unitIndex, 
78                 int totalUnitsKnown,
79                 int maxProblemPerUnit){
80         
81                 this.fileName = compilationUnit.getFileName();
82                 this.compilationUnit = compilationUnit;
83                 this.unitIndex = unitIndex;
84                 this.totalUnitsKnown = totalUnitsKnown;
85                 this.maxProblemPerUnit = maxProblemPerUnit;
86         }
87
88         private int computePriority(IProblem problem){
89         
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;
94                 
95                 int priority = 1000 - problem.getSourceLineNumber(); // early problems first
96                 if (priority < 0) priority = 0;
97                 if (problem.isError()){
98                         priority += P_ERROR;
99                 }
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;
106 //                              }
107 //                      } else {
108                         priority += P_OUTSIDE_METHOD;
109 //                      }
110                 } else {
111                         priority += P_OUTSIDE_METHOD;
112                 }
113                 if (firstErrorsMap.containsKey(problem)){
114                         priority += P_FIRST_ERROR;
115                 }
116                 return priority;
117         }
118
119         
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) {
126                         return problems;
127                 }
128                 if (problemCount == 0) {
129                         return tasks;
130                 }
131
132                 int totalNumberOfProblem = problemCount + taskCount;
133                 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
134                 int allProblemIndex = 0;
135                 int taskIndex = 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];
142                         }
143                         if (problemIndex < problemCount) {
144                                 nextProblem = problems[problemIndex];
145                         }
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;
152                                                 problemIndex++;
153                                         } else {
154                                                 currentProblem = nextTask;
155                                                 taskIndex++;
156                                         }
157                                 } else {
158                                         currentProblem = nextProblem;
159                                         problemIndex++;
160                                 }
161                         } else {
162                                 if (nextTask != null) {
163                                         currentProblem = nextTask;
164                                         taskIndex++;
165                                 }
166                         }
167                         allProblems[allProblemIndex++] = currentProblem;
168                 }
169                 return allProblems;
170         }
171         
172 //      public ClassFile[] getClassFiles() {
173 //              Enumeration enum = compiledTypes.elements();
174 //              ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
175 //              int index = 0;
176 //              while (enum.hasMoreElements()){
177 //                      classFiles[index++] = (ClassFile)enum.nextElement();
178 //              }
179 //              return classFiles;      
180 //      }
181
182         /**
183          * Answer the initial compilation unit corresponding to the present compilation result
184          */
185         public ICompilationUnit getCompilationUnit(){
186                 return compilationUnit;
187         }
188
189         /**
190          * Answer the initial file name
191          */
192         public char[] getFileName(){
193                 return fileName;
194         }
195         
196         /**
197          * Answer the errors encountered during compilation.
198          */
199         public IProblem[] getErrors() {
200         
201                 IProblem[] problems = getProblems();
202                 int errorCount = 0;
203                 for (int i = 0; i < this.problemCount; i++) {
204                         if (problems[i].isError()) errorCount++;
205                 }
206                 if (errorCount == this.problemCount) return problems;
207                 IProblem[] errors = new IProblem[errorCount];
208                 int index = 0;
209                 for (int i = 0; i < this.problemCount; i++) {
210                         if (problems[i].isError()) errors[index++] = problems[i];
211                 }
212                 return errors;
213         }
214         
215         /**
216          * Answer the problems (errors and warnings) encountered during compilation.
217          *
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
221          * problems.
222          */
223         public IProblem[] getProblems() {
224                 
225                 // Re-adjust the size of the problems if necessary.
226                 if (problems != null) {
227         
228                         if (this.problemCount != problems.length) {
229                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
230                         }
231         
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);
236                         }
237         
238                         // Sort problems per source positions.
239                         quickSort(problems, 0, problems.length-1);
240                 }
241                 return problems;
242         }
243
244         /**
245          * Answer the tasks (TO-DO, ...) encountered during compilation.
246          *
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
250          * problems.
251          */
252         public IProblem[] getTasks() {
253                 
254                 // Re-adjust the size of the tasks if necessary.
255                 if (this.tasks != null) {
256         
257                         if (this.taskCount != this.tasks.length) {
258                                 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
259                         }
260                         quickSort(tasks, 0, tasks.length-1);
261                 }
262                 return this.tasks;
263         }
264         
265         public boolean hasErrors() {
266
267                 if (problems != null)
268                         for (int i = 0; i < problemCount; i++) {
269                                 if (problems[i].isError())
270                                         return true;
271                         }
272                 return false;
273         }
274
275         public boolean hasProblems() {
276
277                 return problemCount != 0;
278         }
279
280         public boolean hasSyntaxError(){
281
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())
286                                         return true;
287                         }
288                 return false;
289         }
290
291         public boolean hasTasks() {
292                 return this.taskCount != 0;
293         }
294         
295         public boolean hasWarnings() {
296
297                 if (problems != null)
298                         for (int i = 0; i < problemCount; i++) {
299                                 if (problems[i].isWarning())
300                                         return true;
301                         }
302                 return false;
303         }
304         
305         private static void quickSort(IProblem[] list, int left, int right) {
306
307                 if (left >= right) return;
308         
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();
313                 do {
314                         while (list[left].getSourceStart() < mid)
315                                 left++;
316                         while (mid < list[right].getSourceStart())
317                                 right--;
318                         if (left <= right) {
319                                 IProblem tmp = list[left];
320                                 list[left] = list[right];
321                                 list[right] = tmp;
322                                 left++;
323                                 right--;
324                         }
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);
330         }
331         
332         private void quickPrioritize(IProblem[] list, int left, int right) {
333                 
334                 if (left >= right) return;
335         
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]);
340                 do {
341                         while (computePriority(list[right]) < mid)
342                                 right--;
343                         while (mid < computePriority(list[left]))
344                                 left++;
345                         if (left <= right) {
346                                 IProblem tmp = list[left];
347                                 list[left] = list[right];
348                                 list[right] = tmp;
349                                 left++;
350                                 right--;
351                         }
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);
357         }
358         
359         /**
360          * For now, remember the compiled type using its compound name.
361          */
362 //      public void record(char[] typeName, ClassFile classFile) {
363 //
364 //              compiledTypes.put(typeName, classFile);
365 //      }
366
367         public void record(IProblem newProblem, ReferenceContext referenceContext) {
368
369                 if (newProblem.getID() == IProblem.Task) {
370                         recordTask(newProblem);
371                         return;
372                 }
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);
377                 }
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);
384                 }
385         }
386
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);
392                 }
393                 this.tasks[this.taskCount++] = newProblem;
394         }
395         
396         public CompilationResult tagAsAccepted(){
397
398                 this.hasBeenAccepted = true;
399                 this.problemsMap = null; // flush
400                 return this;
401         }
402         
403         public String toString(){
404
405                 StringBuffer buffer = new StringBuffer();
406                 if (this.fileName != null){
407                         buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
408                 }
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$
415 //                              
416 //                      }
417 //              } else {
418 //                      buffer.append("No COMPILED type\n");  //$NON-NLS-1$
419 //              }
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$
424                         }
425                 } else {
426                         buffer.append("No PROBLEM\n"); //$NON-NLS-1$
427                 } 
428                 return buffer.toString();
429         }
430 }