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
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.
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
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;
48 public class SourceTypeConverter implements CompilerModifiers {
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
56 public static CompilationUnitDeclaration buildCompilationUnit(
57 ISourceType[] sourceTypes,
58 boolean needFieldsAndMethods,
59 boolean needMemberTypes,
60 ProblemReporter problemReporter,
61 CompilationResult compilationResult) {
63 ISourceType sourceType = sourceTypes[0];
64 if (sourceType.getName() == null)
65 return null; // do a basic test that the sourceType is valid
67 CompilationUnitDeclaration compilationUnit =
68 new CompilationUnitDeclaration(problemReporter, compilationResult, 0);
69 // not filled at this point
71 /* only positions available */
72 int start = sourceType.getNameSourceStart();
73 int end = sourceType.getNameSourceEnd();
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);
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);
93 return compilationUnit;
97 * Convert a field source element into a parsed field declaration
99 private static FieldDeclaration convert(ISourceField sourceField) {
101 FieldDeclaration field = new FieldDeclaration();
103 int start = sourceField.getNameSourceStart();
104 int end = sourceField.getNameSourceEnd();
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();
114 /* conversion of field constant: if not present, then cannot generate binary against
115 converted parse nodes */
117 if (field.modifiers & AccFinal){
118 char[] initializationSource = sourceField.getInitializationSource();
125 * Convert a method source element into a parsed method/constructor declaration
127 private static AbstractMethodDeclaration convert(ISourceMethod sourceMethod, CompilationResult compilationResult) {
129 AbstractMethodDeclaration method;
131 /* only source positions available */
132 int start = sourceMethod.getNameSourceStart();
133 int end = sourceMethod.getNameSourceEnd();
135 if (sourceMethod.isConstructor()) {
136 ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
137 decl.isDefaultConstructor = false;
140 MethodDeclaration decl = new MethodDeclaration(compilationResult);
141 /* convert return type */
143 createTypeReference(sourceMethod.getReturnTypeName(), start, end);
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();
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] =
164 createTypeReference(argumentTypeNames[i], start, end),
166 // do not care whether was final or not
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);
181 * Convert a source element type into a parsed type declaration
183 * Can optionally ignore fields & methods
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);
195 type = new MemberTypeDeclaration(compilationResult);
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;
206 /* set superclass and superinterfaces */
207 if (sourceType.getSuperclassName() != null)
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);
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,
230 /* convert fields and methods */
231 if (needFieldsAndMethods) {
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]);
240 /* convert methods - need to add default constructor if necessary */
241 ISourceMethod[] sourceMethods = sourceType.getMethods();
242 int sourceMethodCount = sourceMethods == null ? 0 : sourceMethods.length;
244 /* source type has a constructor ? */
245 /* by default, we assume that one is needed. */
247 for (int i = 0; i < sourceMethodCount; i++) {
248 if (sourceMethods[i].isConstructor()) {
250 // Does not need the extra constructor since one constructor already exists.
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);
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;
264 type.methods[neededCount + i] = method;
271 * Build an import reference from an import name, e.g. java.lang.*
273 private static ImportReference createImportReference(
278 /* count identifiers */
279 int max = importName.length;
281 for (int i = 0; i < max; i++) {
282 if (importName[i] == '.')
285 /* import on demand? */
286 boolean onDemand = importName[max - 1] == '*';
288 identCount++; // one more ident than dots
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;
295 return new ImportReference(
296 CharOperation.splitOn('.', importName, 0, max - (onDemand ? 3 : 1)),
302 * Build a type reference from a readable name, e.g. java.lang.Object[][]
304 private static TypeReference createTypeReference(
305 char[] typeSignature,
309 /* count identifiers and dimensions */
310 int max = typeSignature.length;
314 for (int i = 0; i < max; i++) {
315 switch (typeSignature[i]) {
326 /* rebuild identifiers and dimensions */
327 if (identCount == 1) { // simple type reference
329 return new SingleTypeReference(typeSignature, (((long) start )<< 32) + end);
331 char[] identifier = new char[dimStart];
332 System.arraycopy(typeSignature, 0, identifier, 0, dimStart);
333 return new ArrayTypeReference(identifier, dim, (((long) start) << 32) + end);
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++) {
341 char[][] identifiers =
342 CharOperation.splitOn('.', typeSignature, 0, dimStart - 1);
344 return new QualifiedTypeReference(identifiers, positions);
346 return new ArrayQualifiedTypeReference(identifiers, dim, positions);