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.codeassist.impl;
15 import net.sourceforge.phpdt.internal.codeassist.ISearchableNameEnvironment;
16 import net.sourceforge.phpdt.internal.compiler.*;
17 import net.sourceforge.phpdt.internal.compiler.env.*;
19 import net.sourceforge.phpdt.internal.compiler.ast.*;
20 import net.sourceforge.phpdt.internal.compiler.lookup.*;
21 import net.sourceforge.phpdt.internal.compiler.parser.*;
22 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
23 import net.sourceforge.phpdt.internal.compiler.impl.*;
26 public abstract class Engine implements ITypeRequestor {
28 public LookupEnvironment lookupEnvironment;
30 protected CompilationUnitScope unitScope;
31 protected ISearchableNameEnvironment nameEnvironment;
33 public AssistOptions options;
34 public CompilerOptions compilerOptions;
36 public Engine(Map settings){
37 this.options = new AssistOptions(settings);
38 this.compilerOptions = new CompilerOptions(settings);
42 * Add an additional binary type
44 public void accept(IBinaryType binaryType, PackageBinding packageBinding) {
45 lookupEnvironment.createBinaryTypeFrom(binaryType, packageBinding);
49 * Add an additional compilation unit.
51 public void accept(ICompilationUnit sourceUnit) {
52 CompilationResult result = new CompilationResult(sourceUnit, 1, 1, this.compilerOptions.maxProblemsPerUnit);
53 CompilationUnitDeclaration parsedUnit =
54 this.getParser().dietParse(sourceUnit, result);
57 lookupEnvironment.buildTypeBindings(parsedUnit);
58 lookupEnvironment.completeTypeBindings(parsedUnit, true);
62 * Add additional source types (the first one is the requested type, the rest is formed by the
63 * secondary types defined in the same compilation unit).
65 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding) {
66 CompilationResult result =
67 new CompilationResult(sourceTypes[0].getFileName(), 1, 1, this.compilerOptions.maxProblemsPerUnit);
68 CompilationUnitDeclaration unit =
69 SourceTypeConverter.buildCompilationUnit(
73 lookupEnvironment.problemReporter,
77 lookupEnvironment.buildTypeBindings(unit);
78 lookupEnvironment.completeTypeBindings(unit, true);
82 public abstract AssistParser getParser();
84 protected boolean mustQualifyType(
88 // If there are no types defined into the current CU yet.
89 if (unitScope == null)
92 char[][] compoundPackageName = CharOperation.splitOn('.', packageName);
93 char[] readableTypeName = CharOperation.concat(packageName, typeName, '.');
95 if (CharOperation.equals(unitScope.fPackage.compoundName, compoundPackageName))
98 ImportBinding[] imports = unitScope.imports;
100 for (int i = 0, length = imports.length; i < length; i++) {
101 if (imports[i].onDemand) {
102 if (CharOperation.equals(imports[i].compoundName, compoundPackageName)) {
103 for (int j = 0; j < imports.length; j++) {
105 if(imports[j].onDemand) {
106 if(nameEnvironment.findType(typeName, imports[j].compoundName) != null){
110 if(CharOperation.equals(CharOperation.lastSegment(imports[j].readableName(), '.'), typeName)) {
116 return false; // how do you match p1.p2.A.* ?
121 if (CharOperation.equals(imports[i].readableName(), readableTypeName)) {
129 protected void parseMethod(CompilationUnitDeclaration unit, int position) {
130 for (int i = unit.types.length; --i >= 0;) {
131 TypeDeclaration type = unit.types[i];
132 if (type.declarationSourceStart < position
133 && type.declarationSourceEnd >= position) {
134 getParser().scanner.setSource(
135 unit.compilationResult.compilationUnit.getContents());
136 parseMethod(type, unit, position);
142 private void parseMethod(
143 TypeDeclaration type,
144 CompilationUnitDeclaration unit,
147 TypeDeclaration[] memberTypes = type.memberTypes;
148 if (memberTypes != null) {
149 for (int i = memberTypes.length; --i >= 0;) {
150 TypeDeclaration memberType = memberTypes[i];
151 if (memberType.bodyStart > position)
153 if (memberType.declarationSourceEnd >= position) {
154 parseMethod(memberType, unit, position);
160 AbstractMethodDeclaration[] methods = type.methods;
161 if (methods != null) {
162 for (int i = methods.length; --i >= 0;) {
163 AbstractMethodDeclaration method = methods[i];
164 if (method.bodyStart > position)
166 if (method.declarationSourceEnd >= position) {
167 getParser().parseBlockStatements(method, unit);
173 FieldDeclaration[] fields = type.fields;
174 if (fields != null) {
175 for (int i = fields.length; --i >= 0;) {
176 if (!(fields[i] instanceof Initializer))
178 Initializer initializer = (Initializer) fields[i];
179 if (initializer.bodyStart > position)
181 if (initializer.declarationSourceEnd >= position) {
182 getParser().parseBlockStatements(initializer, type, unit);
189 protected void reset() {
190 lookupEnvironment.reset();