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.parser;
14 * Converter from source element type to parsed compilation unit.
16 * Limitation: | The source element field does not carry any information for its
17 * constant part, thus | the converted parse tree will not include any field
18 * initializations. | Therefore, any binary produced by compiling against
19 * converted source elements will | not take advantage of remote field constant
20 * inlining. | Given the intended purpose of the conversion is to resolve
21 * references, this is not | a problem.
25 import java.util.ArrayList;
27 import net.sourceforge.phpdt.core.compiler.CharOperation;
28 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
29 import net.sourceforge.phpdt.internal.compiler.ast.AbstractMethodDeclaration;
30 import net.sourceforge.phpdt.internal.compiler.ast.Argument;
31 import net.sourceforge.phpdt.internal.compiler.ast.ArrayQualifiedTypeReference;
32 import net.sourceforge.phpdt.internal.compiler.ast.ArrayTypeReference;
33 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
34 import net.sourceforge.phpdt.internal.compiler.ast.ConstructorDeclaration;
35 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
36 import net.sourceforge.phpdt.internal.compiler.ast.ImportReference;
37 import net.sourceforge.phpdt.internal.compiler.ast.MemberTypeDeclaration;
38 import net.sourceforge.phpdt.internal.compiler.ast.MethodDeclaration;
39 import net.sourceforge.phpdt.internal.compiler.ast.QualifiedTypeReference;
40 import net.sourceforge.phpdt.internal.compiler.ast.SingleTypeReference;
41 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
42 import net.sourceforge.phpdt.internal.compiler.ast.TypeReference;
43 import net.sourceforge.phpdt.internal.compiler.env.ISourceField;
44 import net.sourceforge.phpdt.internal.compiler.env.ISourceMethod;
45 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
46 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
47 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
49 public class SourceTypeConverter implements CompilerModifiers {
51 private boolean needFieldInitialization;
53 private CompilationUnitDeclaration unit;
55 private UnitParser parser;
57 private ProblemReporter problemReporter;
59 private SourceTypeConverter(boolean needFieldInitialization,
60 ProblemReporter problemReporter) {
61 this.needFieldInitialization = needFieldInitialization;
62 this.problemReporter = problemReporter;
66 * Convert a set of source element types into a parsed compilation unit
67 * declaration The argument types are then all grouped in the same unit. The
68 * argument types must at least contain one type. Can optionally ignore
69 * fields & methods or member types or field initialization
71 public static CompilationUnitDeclaration buildCompilationUnit(
72 ISourceType[] sourceTypes, boolean needFieldsAndMethods,
73 boolean needMemberTypes, boolean needFieldInitialization,
74 ProblemReporter problemReporter, CompilationResult compilationResult) {
76 return new SourceTypeConverter(needFieldInitialization, problemReporter)
77 .convert(sourceTypes, needFieldsAndMethods, needMemberTypes,
82 * Convert a set of source element types into a parsed compilation unit
83 * declaration The argument types are then all grouped in the same unit. The
84 * argument types must at least contain one type.
86 private CompilationUnitDeclaration convert(ISourceType[] sourceTypes,
87 boolean needFieldsAndMethods, boolean needMemberTypes,
88 CompilationResult compilationResult) {
89 ISourceType sourceType = sourceTypes[0];
90 if (sourceType.getName() == null)
91 return null; // do a basic test that the sourceType is valid
93 this.unit = new CompilationUnitDeclaration(problemReporter,
94 compilationResult, 0);
95 // not filled at this point
97 /* only positions available */
98 int start = sourceType.getNameSourceStart();
99 int end = sourceType.getNameSourceEnd();
101 /* convert package and imports */
102 // if (sourceType.getPackageName() != null
103 // && sourceType.getPackageName().length > 0)
104 // // if its null then it is defined in the default package
105 // this.unit.currentPackage =
106 // createImportReference(sourceType.getPackageName(), start, end);
107 char[][] importNames = sourceType.getImports();
108 int importCount = importNames == null ? 0 : importNames.length;
109 this.unit.imports = new ImportReference[importCount];
110 // for (int i = 0; i < importCount; i++)
111 // this.unit.imports[i] = createImportReference(importNames[i], start,
113 /* convert type(s) */
114 int typeCount = sourceTypes.length;
115 this.unit.types = new ArrayList(typeCount);
116 for (int i = 0; i < typeCount; i++) {
117 this.unit.types.set(i, convert(sourceTypes[i],
118 needFieldsAndMethods, needMemberTypes, compilationResult));
119 // this.unit.types[i] =
120 // convert(sourceTypes[i], needFieldsAndMethods, needMemberTypes,
121 // compilationResult);
127 * Convert a field source element into a parsed field declaration
129 private FieldDeclaration convert(ISourceField sourceField,
130 TypeDeclaration type) {
132 FieldDeclaration field = new FieldDeclaration();
134 int start = sourceField.getNameSourceStart();
135 int end = sourceField.getNameSourceEnd();
137 field.name = sourceField.getName();
138 field.sourceStart = start;
139 field.sourceEnd = end;
140 field.type = createTypeReference(sourceField.getTypeName(), start, end);
141 field.declarationSourceStart = sourceField.getDeclarationSourceStart();
142 field.declarationSourceEnd = sourceField.getDeclarationSourceEnd();
143 field.modifiers = sourceField.getModifiers();
145 if (this.needFieldInitialization) {
146 /* conversion of field constant */
147 char[] initializationSource = sourceField.getInitializationSource();
148 if (initializationSource != null) {
149 if (this.parser == null) {
150 this.parser = new UnitParser(this.problemReporter);
152 // this.problemReporter.options.sourceLevel >=
153 // CompilerOptions.JDK1_4);
155 this.parser.parse(field, type, this.unit, initializationSource);
163 * Convert a method source element into a parsed method/constructor
166 private AbstractMethodDeclaration convert(ISourceMethod sourceMethod,
167 CompilationResult compilationResult) {
169 AbstractMethodDeclaration method;
171 /* only source positions available */
172 int start = sourceMethod.getNameSourceStart();
173 int end = sourceMethod.getNameSourceEnd();
175 if (sourceMethod.isConstructor()) {
176 ConstructorDeclaration decl = new ConstructorDeclaration(
178 decl.isDefaultConstructor = false;
181 MethodDeclaration decl = new MethodDeclaration(compilationResult);
182 /* convert return type */
183 decl.returnType = createTypeReference(sourceMethod
184 .getReturnTypeName(), start, end);
187 method.selector = sourceMethod.getSelector();
188 method.modifiers = sourceMethod.getModifiers();
189 method.sourceStart = start;
190 method.sourceEnd = end;
191 method.declarationSourceStart = sourceMethod
192 .getDeclarationSourceStart();
193 method.declarationSourceEnd = sourceMethod.getDeclarationSourceEnd();
195 /* convert arguments */
196 char[][] argumentTypeNames = sourceMethod.getArgumentTypeNames();
197 char[][] argumentNames = sourceMethod.getArgumentNames();
198 int argumentCount = argumentTypeNames == null ? 0
199 : argumentTypeNames.length;
200 long position = (long) start << 32 + end;
201 method.arguments = new Argument[argumentCount];
202 for (int i = 0; i < argumentCount; i++) {
203 method.arguments[i] = new Argument(argumentNames[i], position,
204 createTypeReference(argumentTypeNames[i], start, end),
206 // do not care whether was final or not
209 /* convert thrown exceptions */
210 char[][] exceptionTypeNames = sourceMethod.getExceptionTypeNames();
211 int exceptionCount = exceptionTypeNames == null ? 0
212 : exceptionTypeNames.length;
213 method.thrownExceptions = new TypeReference[exceptionCount];
214 for (int i = 0; i < exceptionCount; i++) {
215 method.thrownExceptions[i] = createTypeReference(
216 exceptionTypeNames[i], start, end);
222 * Convert a source element type into a parsed type declaration
224 * Can optionally ignore fields & methods
226 private TypeDeclaration convert(ISourceType sourceType,
227 boolean needFieldsAndMethods, boolean needMemberTypes,
228 CompilationResult compilationResult) {
229 /* create type declaration - can be member type */
230 TypeDeclaration type;
231 if (sourceType.getEnclosingType() == null) {
232 type = new TypeDeclaration(compilationResult);
234 type = new MemberTypeDeclaration(compilationResult);
236 type.name = sourceType.getName();
237 int start, end; // only positions available
238 type.sourceStart = start = sourceType.getNameSourceStart();
239 type.sourceEnd = end = sourceType.getNameSourceEnd();
240 type.modifiers = sourceType.getModifiers();
241 type.declarationSourceStart = sourceType.getDeclarationSourceStart();
242 type.declarationSourceEnd = sourceType.getDeclarationSourceEnd();
243 type.bodyEnd = type.declarationSourceEnd;
245 /* set superclass and superinterfaces */
246 if (sourceType.getSuperclassName() != null)
247 type.superclass = createTypeReference(sourceType
248 .getSuperclassName(), start, end);
249 char[][] interfaceNames = sourceType.getInterfaceNames();
250 int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
251 type.superInterfaces = new TypeReference[interfaceCount];
252 for (int i = 0; i < interfaceCount; i++) {
253 type.superInterfaces[i] = createTypeReference(interfaceNames[i],
256 /* convert member types */
257 if (needMemberTypes) {
258 ISourceType[] sourceMemberTypes = sourceType.getMemberTypes();
259 int sourceMemberTypeCount = sourceMemberTypes == null ? 0
260 : sourceMemberTypes.length;
261 type.memberTypes = new MemberTypeDeclaration[sourceMemberTypeCount];
262 for (int i = 0; i < sourceMemberTypeCount; i++) {
263 type.memberTypes[i] = (MemberTypeDeclaration) convert(
264 sourceMemberTypes[i], needFieldsAndMethods, true,
268 /* convert fields and methods */
269 if (needFieldsAndMethods) {
271 ISourceField[] sourceFields = sourceType.getFields();
272 int sourceFieldCount = sourceFields == null ? 0
273 : sourceFields.length;
274 type.fields = new FieldDeclaration[sourceFieldCount];
275 for (int i = 0; i < sourceFieldCount; i++) {
276 type.fields[i] = convert(sourceFields[i], type);
279 /* convert methods - need to add default constructor if necessary */
280 ISourceMethod[] sourceMethods = sourceType.getMethods();
281 int sourceMethodCount = sourceMethods == null ? 0
282 : sourceMethods.length;
284 /* source type has a constructor ? */
285 /* by default, we assume that one is needed. */
287 if (!type.isInterface()) {
289 for (int i = 0; i < sourceMethodCount; i++) {
290 if (sourceMethods[i].isConstructor()) {
292 // Does not need the extra constructor since one
293 // constructor already exists.
298 type.methods = new AbstractMethodDeclaration[sourceMethodCount
300 if (neededCount != 0) { // add default constructor in first position
301 type.methods[0] = type.createsInternalConstructor(false, false);
303 boolean isInterface = type.isInterface();
304 for (int i = 0; i < sourceMethodCount; i++) {
305 AbstractMethodDeclaration method = convert(sourceMethods[i],
307 if (isInterface || method.isAbstract()) { // fix-up flag
308 method.modifiers |= AccSemicolonBody;
310 type.methods[neededCount + i] = method;
317 * Build an import reference from an import name, e.g. java.lang.*
319 // private ImportReference createImportReference(
320 // char[] importName,
324 // /* count identifiers */
325 // int max = importName.length;
326 // int identCount = 0;
327 // for (int i = 0; i < max; i++) {
328 // if (importName[i] == '.')
331 // /* import on demand? */
332 // boolean onDemand = importName[max - 1] == '*';
334 // identCount++; // one more ident than dots
336 // long[] positions = new long[identCount];
337 // long position = (long) start << 32 + end;
338 // for (int i = 0; i < identCount; i++) {
339 // positions[i] = position;
341 // return new ImportReference(
342 // CharOperation.splitOn('.', importName, 0, max - (onDemand ? 2 : 0)),
347 * Build a type reference from a readable name, e.g. java.lang.Object[][]
349 private TypeReference createTypeReference(char[] typeSignature, int start,
352 /* count identifiers and dimensions */
353 int max = typeSignature.length;
357 for (int i = 0; i < max; i++) {
358 switch (typeSignature[i]) {
369 /* rebuild identifiers and dimensions */
370 if (identCount == 1) { // simple type reference
372 return new SingleTypeReference(typeSignature,
373 (((long) start) << 32) + end);
375 char[] identifier = new char[dimStart];
376 System.arraycopy(typeSignature, 0, identifier, 0, dimStart);
377 return new ArrayTypeReference(identifier, dim,
378 (((long) start) << 32) + end);
380 } else { // qualified type reference
381 long[] positions = new long[identCount];
382 long pos = (((long) start) << 32) + end;
383 for (int i = 0; i < identCount; i++) {
386 char[][] identifiers = CharOperation.splitOn('.', typeSignature, 0,
389 return new QualifiedTypeReference(identifiers, positions);
391 return new ArrayQualifiedTypeReference(identifiers, dim,