Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / CompilationResult.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.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 import org.eclipse.jdt.core.compiler.*;
35 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
36 import org.eclipse.jdt.internal.compiler.env.*;
37 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
38
39 import java.util.*;
40
41 public class CompilationResult {
42         
43         public IProblem problems[];
44         public IProblem tasks[];
45         public int problemCount;
46         public int taskCount;
47         public ICompilationUnit compilationUnit;
48         private Map problemsMap;
49         private Map firstErrorsMap;
50         private int maxProblemPerUnit;
51         public char[][][] qualifiedReferences;
52         public char[][] simpleNameReferences;
53
54         public int lineSeparatorPositions[];
55         public Hashtable compiledTypes = new Hashtable(11);
56         public int unitIndex, totalUnitsKnown;
57         public boolean hasBeenAccepted = false;
58         public char[] fileName;
59         
60         public CompilationResult(
61                 char[] fileName,
62                 int unitIndex, 
63                 int totalUnitsKnown,
64                 int maxProblemPerUnit){
65         
66                 this.fileName = fileName;
67                 this.unitIndex = unitIndex;
68                 this.totalUnitsKnown = totalUnitsKnown;
69                 this.maxProblemPerUnit = maxProblemPerUnit;
70         }
71         
72         public CompilationResult(
73                 ICompilationUnit compilationUnit,
74                 int unitIndex, 
75                 int totalUnitsKnown,
76                 int maxProblemPerUnit){
77         
78                 this.fileName = compilationUnit.getFileName();
79                 this.compilationUnit = compilationUnit;
80                 this.unitIndex = unitIndex;
81                 this.totalUnitsKnown = totalUnitsKnown;
82                 this.maxProblemPerUnit = maxProblemPerUnit;
83         }
84
85         private int computePriority(IProblem problem){
86         
87                 final int P_STATIC = 10000;
88                 final int P_OUTSIDE_METHOD = 40000;
89                 final int P_FIRST_ERROR = 20000;
90                 final int P_ERROR = 100000;
91                 
92                 int priority = 10000 - problem.getSourceLineNumber(); // early problems first
93                 if (priority < 0) priority = 0;
94                 if (problem.isError()){
95                         priority += P_ERROR;
96                 }
97                 ReferenceContext context = problemsMap == null ? null : (ReferenceContext) problemsMap.get(problem);
98                 if (context != null){
99                         if (context instanceof AbstractMethodDeclaration){
100                                 AbstractMethodDeclaration method = (AbstractMethodDeclaration) context;
101                                 if (method.isStatic()) {
102                                         priority += P_STATIC;
103                                 }
104                         } else {
105                         priority += P_OUTSIDE_METHOD;
106                         }
107                 } else {
108                         priority += P_OUTSIDE_METHOD;
109                 }
110                 if (firstErrorsMap.containsKey(problem)){
111                         priority += P_FIRST_ERROR;
112                 }
113                 return priority;
114         }
115
116         
117         public IProblem[] getAllProblems() {
118                 IProblem[] onlyProblems = this.getProblems();
119                 int onlyProblemCount = onlyProblems != null ? onlyProblems.length : 0;
120                 IProblem[] onlyTasks = this.getTasks();
121                 int onlyTaskCount = onlyTasks != null ? onlyTasks.length : 0;
122                 if (onlyTaskCount == 0) {
123                         return onlyProblems;
124                 }
125                 if (onlyProblemCount == 0) {
126                         return onlyTasks;
127                 }
128
129                 int totalNumberOfProblem = onlyProblemCount + onlyTaskCount;
130                 IProblem[] allProblems = new IProblem[totalNumberOfProblem];
131                 int allProblemIndex = 0;
132                 int taskIndex = 0;
133                 int problemIndex = 0;
134                 while (taskIndex + problemIndex < totalNumberOfProblem) {
135                         IProblem nextTask = null;
136                         IProblem nextProblem = null;
137                         if (taskIndex < onlyTaskCount) {
138                                 nextTask = onlyTasks[taskIndex];
139                         }
140                         if (problemIndex < onlyProblemCount) {
141                                 nextProblem = onlyProblems[problemIndex];
142                         }
143                         // select the next problem
144                         IProblem currentProblem = null;
145                         if (nextProblem != null) {
146                                 if (nextTask != null) {
147                                         if (nextProblem.getSourceStart() < nextTask.getSourceStart()) {
148                                                 currentProblem = nextProblem;
149                                                 problemIndex++;
150                                         } else {
151                                                 currentProblem = nextTask;
152                                                 taskIndex++;
153                                         }
154                                 } else {
155                                         currentProblem = nextProblem;
156                                         problemIndex++;
157                                 }
158                         } else {
159                                 if (nextTask != null) {
160                                         currentProblem = nextTask;
161                                         taskIndex++;
162                                 }
163                         }
164                         allProblems[allProblemIndex++] = currentProblem;
165                 }
166                 return allProblems;
167         }
168         
169         public ClassFile[] getClassFiles() {
170                 Enumeration files = compiledTypes.elements();
171                 ClassFile[] classFiles = new ClassFile[compiledTypes.size()];
172                 int index = 0;
173                 while (files.hasMoreElements()){
174                         classFiles[index++] = (ClassFile)files.nextElement();
175                 }
176                 return classFiles;      
177         }
178
179         /**
180          * Answer the initial compilation unit corresponding to the present compilation result
181          */
182         public ICompilationUnit getCompilationUnit(){
183                 return compilationUnit;
184         }
185
186         /**
187          * Answer the initial file name
188          */
189         public char[] getFileName(){
190                 return fileName;
191         }
192         
193         /**
194          * Answer the errors encountered during compilation.
195          */
196         public IProblem[] getErrors() {
197         
198                 IProblem[] reportedProblems = getProblems();
199                 int errorCount = 0;
200                 for (int i = 0; i < this.problemCount; i++) {
201                         if (reportedProblems[i].isError()) errorCount++;
202                 }
203                 if (errorCount == this.problemCount) return reportedProblems;
204                 IProblem[] errors = new IProblem[errorCount];
205                 int index = 0;
206                 for (int i = 0; i < this.problemCount; i++) {
207                         if (reportedProblems[i].isError()) errors[index++] = reportedProblems[i];
208                 }
209                 return errors;
210         }
211         
212         /**
213          * Answer the problems (errors and warnings) encountered during compilation.
214          *
215          * This is not a compiler internal API - it has side-effects !
216          * It is intended to be used only once all problems have been detected,
217          * and makes sure the problems slot as the exact size of the number of
218          * problems.
219          */
220         public IProblem[] getProblems() {
221                 
222                 // Re-adjust the size of the problems if necessary.
223                 if (problems != null) {
224         
225                         if (this.problemCount != problems.length) {
226                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
227                         }
228         
229                         if (this.maxProblemPerUnit > 0 && this.problemCount > this.maxProblemPerUnit){
230                                 quickPrioritize(problems, 0, problemCount - 1);
231                                 this.problemCount = this.maxProblemPerUnit;
232                                 System.arraycopy(problems, 0, (problems = new IProblem[problemCount]), 0, problemCount);
233                         }
234         
235                         // Sort problems per source positions.
236                         quickSort(problems, 0, problems.length-1);
237                 }
238                 return problems;
239         }
240
241         /**
242          * Answer the tasks (TO-DO, ...) encountered during compilation.
243          *
244          * This is not a compiler internal API - it has side-effects !
245          * It is intended to be used only once all problems have been detected,
246          * and makes sure the problems slot as the exact size of the number of
247          * problems.
248          */
249         public IProblem[] getTasks() {
250                 
251                 // Re-adjust the size of the tasks if necessary.
252                 if (this.tasks != null) {
253         
254                         if (this.taskCount != this.tasks.length) {
255                                 System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount]), 0, this.taskCount);
256                         }
257                         quickSort(tasks, 0, tasks.length-1);
258                 }
259                 return this.tasks;
260         }
261         
262         public boolean hasErrors() {
263
264                 if (problems != null)
265                         for (int i = 0; i < problemCount; i++) {
266                                 if (problems[i].isError())
267                                         return true;
268                         }
269                 return false;
270         }
271
272         public boolean hasProblems() {
273
274                 return problemCount != 0;
275         }
276
277         public boolean hasSyntaxError(){
278
279                 if (problems != null)
280                         for (int i = 0; i < problemCount; i++) {
281                                 IProblem problem = problems[i];
282                                 if ((problem.getID() & IProblem.Syntax) != 0 && problem.isError())
283                                         return true;
284                         }
285                 return false;
286         }
287
288         public boolean hasTasks() {
289                 return this.taskCount != 0;
290         }
291         
292         public boolean hasWarnings() {
293
294                 if (problems != null)
295                         for (int i = 0; i < problemCount; i++) {
296                                 if (problems[i].isWarning())
297                                         return true;
298                         }
299                 return false;
300         }
301         
302         private static void quickSort(IProblem[] list, int left, int right) {
303
304                 if (left >= right) return;
305         
306                 // sort the problems by their source start position... starting with 0
307                 int original_left = left;
308                 int original_right = right;
309                 int mid = list[(left + right) / 2].getSourceStart();
310                 do {
311                         while (list[left].getSourceStart() < mid)
312                                 left++;
313                         while (mid < list[right].getSourceStart())
314                                 right--;
315                         if (left <= right) {
316                                 IProblem tmp = list[left];
317                                 list[left] = list[right];
318                                 list[right] = tmp;
319                                 left++;
320                                 right--;
321                         }
322                 } while (left <= right);
323                 if (original_left < right)
324                         quickSort(list, original_left, right);
325                 if (left < original_right)
326                         quickSort(list, left, original_right);
327         }
328         
329         private void quickPrioritize(IProblem[] list, int left, int right) {
330                 
331                 if (left >= right) return;
332         
333                 // sort the problems by their priority... starting with the highest priority
334                 int original_left = left;
335                 int original_right = right;
336                 int mid = computePriority(list[(left + right) / 2]);
337                 do {
338                         while (computePriority(list[right]) < mid)
339                                 right--;
340                         while (mid < computePriority(list[left]))
341                                 left++;
342                         if (left <= right) {
343                                 IProblem tmp = list[left];
344                                 list[left] = list[right];
345                                 list[right] = tmp;
346                                 left++;
347                                 right--;
348                         }
349                 } while (left <= right);
350                 if (original_left < right)
351                         quickPrioritize(list, original_left, right);
352                 if (left < original_right)
353                         quickPrioritize(list, left, original_right);
354         }
355         
356         /**
357          * For now, remember the compiled type using its compound name.
358          */
359         public void record(char[] typeName, ClassFile classFile) {
360
361                 compiledTypes.put(typeName, classFile);
362         }
363
364         public void record(IProblem newProblem, ReferenceContext referenceContext) {
365
366                 if (newProblem.getID() == IProblem.Task) {
367                         recordTask(newProblem);
368                         return;
369                 }
370                 if (problemCount == 0) {
371                         problems = new IProblem[5];
372                 } else if (problemCount == problems.length) {
373                         System.arraycopy(problems, 0, (problems = new IProblem[problemCount * 2]), 0, problemCount);
374                 }
375                 problems[problemCount++] = newProblem;
376                 if (referenceContext != null){
377                         if (problemsMap == null) problemsMap = new Hashtable(5);
378                         if (firstErrorsMap == null) firstErrorsMap = new Hashtable(5);
379                         if (newProblem.isError() && !referenceContext.hasErrors()) firstErrorsMap.put(newProblem, newProblem);
380                         problemsMap.put(newProblem, referenceContext);
381                 }
382         }
383
384         private void recordTask(IProblem newProblem) {
385                 if (this.taskCount == 0) {
386                         this.tasks = new IProblem[5];
387                 } else if (this.taskCount == this.tasks.length) {
388                         System.arraycopy(this.tasks, 0, (this.tasks = new IProblem[this.taskCount * 2]), 0, this.taskCount);
389                 }
390                 this.tasks[this.taskCount++] = newProblem;
391         }
392         
393         public CompilationResult tagAsAccepted(){
394
395                 this.hasBeenAccepted = true;
396                 this.problemsMap = null; // flush
397                 return this;
398         }
399         
400         public String toString(){
401
402                 StringBuffer buffer = new StringBuffer();
403                 if (this.fileName != null){
404                         buffer.append("Filename : ").append(this.fileName).append('\n'); //$NON-NLS-1$
405                 }
406                 if (this.compiledTypes != null){
407                         buffer.append("COMPILED type(s) \n");  //$NON-NLS-1$
408                         Enumeration typeNames = this.compiledTypes.keys();
409                         while (typeNames.hasMoreElements()) {
410                                 char[] typeName = (char[]) typeNames.nextElement();
411                                 buffer.append("\t - ").append(typeName).append('\n');   //$NON-NLS-1$
412                                 
413                         }
414                 } else {
415                         buffer.append("No COMPILED type\n");  //$NON-NLS-1$
416                 }
417                 if (problems != null){
418                         buffer.append(this.problemCount).append(" PROBLEM(s) detected \n"); //$NON-NLS-1$//$NON-NLS-2$
419                         for (int i = 0; i < this.problemCount; i++){
420                                 buffer.append("\t - ").append(this.problems[i]).append('\n'); //$NON-NLS-1$
421                         }
422                 } else {
423                         buffer.append("No PROBLEM\n"); //$NON-NLS-1$
424                 } 
425                 return buffer.toString();
426         }
427 }