422c4b8dcc2594b09796dbe6050f14562bcdb1d6
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.parser;
12
13 /**
14  * Converter from source element type to parsed compilation unit.
15  *
16  * Limitation:
17  * | The source element field does not carry any information for its constant part, thus
18  * | the converted parse tree will not include any field initializations.
19  * | Therefore, any binary produced by compiling against converted source elements will
20  * | not take advantage of remote field constant inlining.
21  * | Given the intended purpose of the conversion is to resolve references, this is not
22  * | a problem.
23  *
24  */
25
26 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
27 import net.sourceforge.phpdt.internal.compiler.ast.AbstractMethodDeclaration;
28 import net.sourceforge.phpdt.internal.compiler.ast.Argument;
29 import net.sourceforge.phpdt.internal.compiler.ast.ArrayQualifiedTypeReference;
30 import net.sourceforge.phpdt.internal.compiler.ast.ArrayTypeReference;
31 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
32 import net.sourceforge.phpdt.internal.compiler.ast.ConstructorDeclaration;
33 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
34 import net.sourceforge.phpdt.internal.compiler.ast.ImportReference;
35 import net.sourceforge.phpdt.internal.compiler.ast.MemberTypeDeclaration;
36 import net.sourceforge.phpdt.internal.compiler.ast.MethodDeclaration;
37 import net.sourceforge.phpdt.internal.compiler.ast.QualifiedTypeReference;
38 import net.sourceforge.phpdt.internal.compiler.ast.SingleTypeReference;
39 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
40 import net.sourceforge.phpdt.internal.compiler.ast.TypeReference;
41 import net.sourceforge.phpdt.internal.compiler.env.ISourceField;
42 import net.sourceforge.phpdt.internal.compiler.env.ISourceMethod;
43 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
44 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
45 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
46 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
47
48 public class SourceTypeConverter implements CompilerModifiers {
49
50         /*
51          * Convert a set of source element types into a parsed compilation unit declaration
52          * The argument types are then all grouped in the same unit. The argument types must 
53          * at least contain one type.
54          * Can optionally ignore fields & methods or member types
55          */
56         public static CompilationUnitDeclaration buildCompilationUnit(
57                 ISourceType[] sourceTypes,
58                 boolean needFieldsAndMethods,
59                 boolean needMemberTypes,
60                 ProblemReporter problemReporter,
61                 CompilationResult compilationResult) {
62
63                 ISourceType sourceType = sourceTypes[0];
64                 if (sourceType.getName() == null)
65                         return null; // do a basic test that the sourceType is valid
66
67                 CompilationUnitDeclaration compilationUnit =
68                         new CompilationUnitDeclaration(problemReporter, compilationResult, 0);
69                 // not filled at this point
70
71                 /* only positions available */
72                 int start = sourceType.getNameSourceStart();
73                 int end = sourceType.getNameSourceEnd();
74
75                 /* convert package and imports */
76                 if (sourceType.getPackageName() != null
77                         && sourceType.getPackageName().length > 0)
78                         // if its null then it is defined in the default package
79                         compilationUnit.currentPackage =
80                                 createImportReference(sourceType.getPackageName(), start, end);
81                 char[][] importNames = sourceType.getImports();
82                 int importCount = importNames == null ? 0 : importNames.length;
83                 compilationUnit.imports = new ImportReference[importCount];
84                 for (int i = 0; i < importCount; i++)
85                         compilationUnit.imports[i] = createImportReference(importNames[i], start, end);
86                 /* convert type(s) */
87                 int typeCount = sourceTypes.length;
88                 compilationUnit.types = new TypeDeclaration[typeCount];
89                 for (int i = 0; i < typeCount; i++) {
90                         compilationUnit.types[i] =
91                                 convert(sourceTypes[i], needFieldsAndMethods, needMemberTypes, compilationResult);
92                 }
93                 return compilationUnit;
94         }
95         
96         /*
97          * Convert a field source element into a parsed field declaration
98          */
99         private static FieldDeclaration convert(ISourceField sourceField) {
100
101                 FieldDeclaration field = new FieldDeclaration();
102
103                 int start = sourceField.getNameSourceStart();
104                 int end = sourceField.getNameSourceEnd();
105
106                 field.name = sourceField.getName();
107                 field.sourceStart = start;
108                 field.sourceEnd = end;
109                 field.type = createTypeReference(sourceField.getTypeName(), start, end);
110                 field.declarationSourceStart = sourceField.getDeclarationSourceStart();
111                 field.declarationSourceEnd = sourceField.getDeclarationSourceEnd();
112                 field.modifiers = sourceField.getModifiers();
113
114                 /* conversion of field constant: if not present, then cannot generate binary against 
115                         converted parse nodes */
116                 /*
117                 if (field.modifiers & AccFinal){
118                         char[] initializationSource = sourceField.getInitializationSource();
119                 }
120                 */
121                 return field;
122         }
123
124         /*
125          * Convert a method source element into a parsed method/constructor declaration 
126          */
127         private static AbstractMethodDeclaration convert(ISourceMethod sourceMethod, CompilationResult compilationResult) {
128
129                 AbstractMethodDeclaration method;
130
131                 /* only source positions available */
132                 int start = sourceMethod.getNameSourceStart();
133                 int end = sourceMethod.getNameSourceEnd();
134
135                 if (sourceMethod.isConstructor()) {
136                         ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
137                         decl.isDefaultConstructor = false;
138                         method = decl;
139                 } else {
140                         MethodDeclaration decl = new MethodDeclaration(compilationResult);
141                         /* convert return type */
142                         decl.returnType =
143                                 createTypeReference(sourceMethod.getReturnTypeName(), start, end);
144                         method = decl;
145                 }
146                 method.selector = sourceMethod.getSelector();
147                 method.modifiers = sourceMethod.getModifiers();
148                 method.sourceStart = start;
149                 method.sourceEnd = end;
150                 method.declarationSourceStart = sourceMethod.getDeclarationSourceStart();
151                 method.declarationSourceEnd = sourceMethod.getDeclarationSourceEnd();
152
153                 /* convert arguments */
154                 char[][] argumentTypeNames = sourceMethod.getArgumentTypeNames();
155                 char[][] argumentNames = sourceMethod.getArgumentNames();
156                 int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length;
157                 long position = (long) start << 32 + end;
158                 method.arguments = new Argument[argumentCount];
159                 for (int i = 0; i < argumentCount; i++) {
160                         method.arguments[i] =
161                                 new Argument(
162                                         argumentNames[i],
163                                         position,
164                                         createTypeReference(argumentTypeNames[i], start, end),
165                                         AccDefault);
166                         // do not care whether was final or not
167                 }
168
169                 /* convert thrown exceptions */
170                 char[][] exceptionTypeNames = sourceMethod.getExceptionTypeNames();
171                 int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
172                 method.thrownExceptions = new TypeReference[exceptionCount];
173                 for (int i = 0; i < exceptionCount; i++) {
174                         method.thrownExceptions[i] =
175                                 createTypeReference(exceptionTypeNames[i], start, end);
176                 }
177                 return method;
178         }
179
180         /*
181          * Convert a source element type into a parsed type declaration
182          *
183          * Can optionally ignore fields & methods
184          */
185         private static TypeDeclaration convert(
186                 ISourceType sourceType,
187                 boolean needFieldsAndMethods,
188                 boolean needMemberTypes,
189                 CompilationResult compilationResult) {
190                 /* create type declaration - can be member type */
191                 TypeDeclaration type;
192                 if (sourceType.getEnclosingType() == null) {
193                         type = new TypeDeclaration(compilationResult);
194                 } else {
195                         type = new MemberTypeDeclaration(compilationResult);
196                 }
197                 type.name = sourceType.getName();
198                 int start, end; // only positions available
199                 type.sourceStart = start = sourceType.getNameSourceStart();
200                 type.sourceEnd = end = sourceType.getNameSourceEnd();
201                 type.modifiers = sourceType.getModifiers();
202                 type.declarationSourceStart = sourceType.getDeclarationSourceStart();
203                 type.declarationSourceEnd = sourceType.getDeclarationSourceEnd();
204                 type.bodyEnd = type.declarationSourceEnd;
205
206                 /* set superclass and superinterfaces */
207                 if (sourceType.getSuperclassName() != null)
208                         type.superclass =
209                                 createTypeReference(sourceType.getSuperclassName(), start, end);
210                 char[][] interfaceNames = sourceType.getInterfaceNames();
211                 int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
212                 type.superInterfaces = new TypeReference[interfaceCount];
213                 for (int i = 0; i < interfaceCount; i++) {
214                         type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end);
215                 }
216                 /* convert member types */
217                 if (needMemberTypes) {
218                         ISourceType[] sourceMemberTypes = sourceType.getMemberTypes();
219                         int sourceMemberTypeCount =
220                                 sourceMemberTypes == null ? 0 : sourceMemberTypes.length;
221                         type.memberTypes = new MemberTypeDeclaration[sourceMemberTypeCount];
222                         for (int i = 0; i < sourceMemberTypeCount; i++) {
223                                 type.memberTypes[i] =
224                                         (MemberTypeDeclaration) convert(sourceMemberTypes[i],
225                                                 needFieldsAndMethods,
226                                                 true,
227                                                 compilationResult);
228                         }
229                 }
230                 /* convert fields and methods */
231                 if (needFieldsAndMethods) {
232                         /* convert fields */
233                         ISourceField[] sourceFields = sourceType.getFields();
234                         int sourceFieldCount = sourceFields == null ? 0 : sourceFields.length;
235                         type.fields = new FieldDeclaration[sourceFieldCount];
236                         for (int i = 0; i < sourceFieldCount; i++) {
237                                 type.fields[i] = convert(sourceFields[i]);
238                         }
239
240                         /* convert methods - need to add default constructor if necessary */
241                         ISourceMethod[] sourceMethods = sourceType.getMethods();
242                         int sourceMethodCount = sourceMethods == null ? 0 : sourceMethods.length;
243
244                         /* source type has a constructor ?           */
245                         /* by default, we assume that one is needed. */
246                         int neededCount = 1;
247                         for (int i = 0; i < sourceMethodCount; i++) {
248                                 if (sourceMethods[i].isConstructor()) {
249                                         neededCount = 0;
250                                         // Does not need the extra constructor since one constructor already exists.
251                                         break;
252                                 }
253                         }
254                         type.methods = new AbstractMethodDeclaration[sourceMethodCount + neededCount];
255                         if (neededCount != 0) { // add default constructor in first position
256                                 type.methods[0] = type.createsInternalConstructor(false, false);
257                         }
258                         boolean isInterface = type.isInterface();
259                         for (int i = 0; i < sourceMethodCount; i++) {
260                                 AbstractMethodDeclaration method =convert(sourceMethods[i], compilationResult);
261                                 if (isInterface || method.isAbstract()) { // fix-up flag 
262                                         method.modifiers |= AccSemicolonBody;
263                                 }
264                                 type.methods[neededCount + i] = method;
265                         }
266                 }
267                 return type;
268         }
269
270         /*
271          * Build an import reference from an import name, e.g. java.lang.*
272          */
273         private static ImportReference createImportReference(
274                 char[] importName,
275                 int start,
276                 int end) {
277
278                 /* count identifiers */
279                 int max = importName.length;
280                 int identCount = 0;
281                 for (int i = 0; i < max; i++) {
282                         if (importName[i] == '.')
283                                 identCount++;
284                 }
285                 /* import on demand? */
286                 boolean onDemand = importName[max - 1] == '*';
287                 if (!onDemand)
288                         identCount++; // one more ident than dots
289
290                 long[] positions = new long[identCount];
291                 long position = (long) start << 32 + end;
292                 for (int i = 0; i < identCount; i++) {
293                         positions[i] = position;
294                 }
295                 return new ImportReference(
296                         CharOperation.splitOn('.', importName, 0, max - (onDemand ? 3 : 1)),
297                         positions,
298                         onDemand);
299         }
300
301         /*
302          * Build a type reference from a readable name, e.g. java.lang.Object[][]
303          */
304         private static TypeReference createTypeReference(
305                 char[] typeSignature,
306                 int start,
307                 int end) {
308
309                 /* count identifiers and dimensions */
310                 int max = typeSignature.length;
311                 int dimStart = max;
312                 int dim = 0;
313                 int identCount = 1;
314                 for (int i = 0; i < max; i++) {
315                         switch (typeSignature[i]) {
316                                 case '[' :
317                                         if (dim == 0)
318                                                 dimStart = i;
319                                         dim++;
320                                         break;
321                                 case '.' :
322                                         identCount++;
323                                         break;
324                         }
325                 }
326                 /* rebuild identifiers and dimensions */
327                 if (identCount == 1) { // simple type reference
328                         if (dim == 0) {
329                                 return new SingleTypeReference(typeSignature, (((long) start )<< 32) + end);
330                         } else {
331                                 char[] identifier = new char[dimStart];
332                                 System.arraycopy(typeSignature, 0, identifier, 0, dimStart);
333                                 return new ArrayTypeReference(identifier, dim, (((long) start) << 32) + end);
334                         }
335                 } else { // qualified type reference
336                         long[] positions = new long[identCount];
337                         long pos = (((long) start) << 32) + end;
338                         for (int i = 0; i < identCount; i++) {
339                                 positions[i] = pos;
340                         }
341                         char[][] identifiers =
342                                 CharOperation.splitOn('.', typeSignature, 0, dimStart - 1);
343                         if (dim == 0) {
344                                 return new QualifiedTypeReference(identifiers, positions);
345                         } else {
346                                 return new ArrayQualifiedTypeReference(identifiers, dim, positions);
347                         }
348                 }
349         }
350 }