aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzzhongwei2012-08-01 23:08:02 (EDT)
committerzzhongwei2012-08-01 23:08:02 (EDT)
commit07f1ebdef4d70983fc034854c60502407ea16bfd (patch)
treeb628c8d8d6c2bd4b1061ae3cceb3ef3209e7a5ac
parent695aa7142d1c1a584e4ace577c0bc3135d821a02 (diff)
downloadorg.eclipse.pdt-07f1ebdef4d70983fc034854c60502407ea16bfd.zip
org.eclipse.pdt-07f1ebdef4d70983fc034854c60502407ea16bfd.tar.gz
org.eclipse.pdt-07f1ebdef4d70983fc034854c60502407ea16bfd.tar.bz2
Bug 385837 - Support Traits in Content-Assist
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/ContextFinder.java47
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/MethodContext.java11
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/ClassVariableDeclarationEvaluator.java64
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/InstanceCreationEvaluator.java14
-rw-r--r--plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java21
5 files changed, 113 insertions, 44 deletions
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/ContextFinder.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/ContextFinder.java
index 004b415..ab7d274 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/ContextFinder.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/ContextFinder.java
@@ -21,12 +21,16 @@ import org.eclipse.dltk.ast.declarations.MethodDeclaration;
import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.core.ISourceModule;
+import org.eclipse.dltk.core.IType;
import org.eclipse.dltk.evaluation.types.UnknownType;
import org.eclipse.dltk.ti.IContext;
import org.eclipse.dltk.ti.ISourceModuleContext;
import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.php.internal.core.compiler.ast.nodes.NamespaceDeclaration;
+import org.eclipse.php.internal.core.compiler.ast.nodes.TraitDeclaration;
import org.eclipse.php.internal.core.typeinference.PHPClassType;
+import org.eclipse.php.internal.core.typeinference.PHPThisClassType;
+import org.eclipse.php.internal.core.typeinference.evaluators.PHPTraitType;
/**
* This abstract AST visitor finds type inference context
@@ -38,10 +42,20 @@ public abstract class ContextFinder extends ASTVisitor {
protected Stack<IContext> contextStack = new Stack<IContext>();
private ISourceModule sourceModule;
+ IType declaringType;
+ IType realType;
+
public ContextFinder(ISourceModule sourceModule) {
this.sourceModule = sourceModule;
}
+ public ContextFinder(ISourceModule sourceModule, IType realType,
+ IType declaringType) {
+ this.sourceModule = sourceModule;
+ this.declaringType = declaringType;
+ this.realType = realType;
+ }
+
/**
* This method must return found context
*
@@ -73,11 +87,36 @@ public abstract class ContextFinder extends ASTVisitor {
PHPClassType instanceType;
if (parentContext instanceof INamespaceContext
&& ((INamespaceContext) parentContext).getNamespace() != null) {
- instanceType = new PHPClassType(
- ((INamespaceContext) parentContext).getNamespace(),
- node.getName());
+ if (node instanceof TraitDeclaration) {
+ instanceType = new PHPTraitType(
+ ((INamespaceContext) parentContext).getNamespace(),
+ node.getName());
+ } else {
+ instanceType = new PHPClassType(
+ ((INamespaceContext) parentContext).getNamespace(),
+ node.getName());
+ }
} else {
- instanceType = new PHPClassType(node.getName());
+ if (node instanceof TraitDeclaration) {
+ if (declaringType != null
+ && realType != null
+ && declaringType.getElementName().equals(
+ node.getName())) {
+ if (realType.getParent() instanceof IType) {
+ IType ns = (IType) realType.getParent();
+ instanceType = new PHPThisClassType(
+ ns.getElementName(),
+ realType.getElementName(), realType);
+ } else {
+ instanceType = new PHPThisClassType(
+ realType.getElementName(), realType);
+ }
+ } else {
+ instanceType = new PHPTraitType(node.getName());
+ }
+ } else {
+ instanceType = new PHPClassType(node.getName());
+ }
}
contextStack.push(new TypeContext(parentContext, instanceType));
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/MethodContext.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/MethodContext.java
index ebd4f02..836d9f1 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/MethodContext.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/context/MethodContext.java
@@ -22,6 +22,7 @@ import org.eclipse.dltk.ti.IInstanceContext;
import org.eclipse.dltk.ti.ISourceModuleContext;
import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.php.internal.core.typeinference.IModelAccessCache;
+import org.eclipse.php.internal.core.typeinference.PHPThisClassType;
/**
* This is a PHP method or function context.
@@ -176,6 +177,16 @@ public class MethodContext implements IContext, INamespaceContext,
public void setCurrentType(IType type) {
this.type = type;
+ if (type != null) {
+ if (type.getParent() instanceof IType) {
+ IType ns = (IType) type.getParent();
+ instanceType = new PHPThisClassType(ns.getElementName(),
+ type.getElementName(), type);
+ } else {
+ instanceType = new PHPThisClassType(type.getElementName(), type);
+ }
+
+ }
}
public IType getType() {
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/ClassVariableDeclarationEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/ClassVariableDeclarationEvaluator.java
index f63b199..97d725b 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/ClassVariableDeclarationEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/ClassVariableDeclarationEvaluator.java
@@ -56,8 +56,8 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
if (types == null) {
TypeContext context = (TypeContext) typedGoal.getContext();
- types = PHPTypeInferenceUtils.getModelElements(context
- .getInstanceType(), context);
+ types = PHPTypeInferenceUtils.getModelElements(
+ context.getInstanceType(), context);
}
if (types == null) {
return null;
@@ -80,11 +80,11 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
}
IField[] fields = PHPModelUtils.getTypeHierarchyField(type,
hierarchy, variableName, true, null);
- Set<IType> fieldDeclaringTypeSet = new HashSet<IType>();
+ Map<IType, IType> fieldDeclaringTypeSet = new HashMap<IType, IType>();
for (IField field : fields) {
IType declaringType = field.getDeclaringType();
if (declaringType != null) {
- fieldDeclaringTypeSet.add(declaringType);
+ fieldDeclaringTypeSet.put(declaringType, type);
ISourceModule sourceModule = declaringType
.getSourceModule();
ModuleDeclaration moduleDeclaration = SourceParserUtil
@@ -100,9 +100,10 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
.getSourceRange();
ClassDeclarationSearcher searcher = new ClassDeclarationSearcher(
- sourceModule, typeDeclaration, sourceRange
- .getOffset(), sourceRange
- .getLength(), null);
+ sourceModule, typeDeclaration,
+ sourceRange.getOffset(),
+ sourceRange.getLength(), null, type,
+ declaringType);
try {
moduleDeclaration.traverse(searcher);
if (searcher.getResult() != null) {
@@ -120,15 +121,17 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
}
if (subGoals.size() == 0) {
- getGoalFromStaticDeclaration(variableName, subGoals, type);
+ getGoalFromStaticDeclaration(variableName, subGoals, type,
+ null);
}
fieldDeclaringTypeSet.remove(type);
if (subGoals.size() == 0 && !fieldDeclaringTypeSet.isEmpty()) {
- for (Iterator iterator = fieldDeclaringTypeSet.iterator(); iterator
- .hasNext();) {
+ for (Iterator iterator = fieldDeclaringTypeSet.keySet()
+ .iterator(); iterator.hasNext();) {
IType fieldDeclaringType = (IType) iterator.next();
getGoalFromStaticDeclaration(variableName, subGoals,
- fieldDeclaringType);
+ fieldDeclaringType,
+ fieldDeclaringTypeSet.get(fieldDeclaringType));
}
}
} catch (CoreException e) {
@@ -144,17 +147,25 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
}
protected void getGoalFromStaticDeclaration(String variableName,
- final List<IGoal> subGoals, final IType type) throws ModelException {
- ISourceModule sourceModule = type.getSourceModule();
+ final List<IGoal> subGoals, final IType declaringType,
+ IType realType) throws ModelException {
+ ISourceModule sourceModule = declaringType.getSourceModule();
ModuleDeclaration moduleDeclaration = SourceParserUtil
.getModuleDeclaration(sourceModule);
TypeDeclaration typeDeclaration = PHPModelUtils.getNodeByClass(
- moduleDeclaration, type);
+ moduleDeclaration, declaringType);
// try to search declarations of type "self::$var =" or
// "$this->var ="
- ClassDeclarationSearcher searcher = new ClassDeclarationSearcher(
- sourceModule, typeDeclaration, 0, 0, variableName);
+ ClassDeclarationSearcher searcher;
+ if (realType != null) {
+ searcher = new ClassDeclarationSearcher(sourceModule,
+ typeDeclaration, 0, 0, variableName, realType,
+ declaringType);
+ } else {
+ searcher = new ClassDeclarationSearcher(sourceModule,
+ typeDeclaration, 0, 0, variableName);
+ }
try {
moduleDeclaration.traverse(searcher);
Map<ASTNode, IContext> staticDeclarations = searcher
@@ -281,6 +292,19 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
this.staticDeclarations = new HashMap<ASTNode, IContext>();
}
+ public ClassDeclarationSearcher(ISourceModule sourceModule,
+ TypeDeclaration typeDeclaration, int offset, int length,
+ String variableName, IType realType, IType declaringType) {
+ // this(sourceModule, typeDeclaration2, offset2, length2,
+ // variableName);
+ super(sourceModule, realType, declaringType);
+ this.typeDeclaration = typeDeclaration;
+ this.offset = offset;
+ this.length = length;
+ this.variableName = variableName;
+ this.staticDeclarations = new HashMap<ASTNode, IContext>();
+ }
+
public ASTNode getResult() {
return result;
}
@@ -332,8 +356,8 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
&& variableName
.equals(((VariableReference) field)
.getName())) {
- staticDeclarations.put(right, contextStack
- .peek());
+ staticDeclarations.put(right,
+ contextStack.peek());
}
}
} else if (left instanceof FieldAccess) {
@@ -346,8 +370,8 @@ public class ClassVariableDeclarationEvaluator extends AbstractPHPGoalEvaluator
&& variableName
.equals('$' + ((SimpleReference) field)
.getName())) {
- staticDeclarations.put(right, contextStack
- .peek());
+ staticDeclarations.put(right,
+ contextStack.peek());
}
}
}
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/InstanceCreationEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/InstanceCreationEvaluator.java
index df8dad7..2262a51 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/InstanceCreationEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/InstanceCreationEvaluator.java
@@ -20,6 +20,7 @@ import org.eclipse.dltk.ti.goals.GoalEvaluator;
import org.eclipse.dltk.ti.goals.IGoal;
import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.php.internal.core.compiler.ast.nodes.ClassInstanceCreation;
+import org.eclipse.php.internal.core.typeinference.context.MethodContext;
public class InstanceCreationEvaluator extends GoalEvaluator {
@@ -34,9 +35,16 @@ public class InstanceCreationEvaluator extends GoalEvaluator {
ClassInstanceCreation expression = (ClassInstanceCreation) typedGoal
.getExpression();
Expression className = expression.getClassName();
- if (className instanceof TypeReference) {
- return new IGoal[] { new ExpressionTypeGoal(goal.getContext(),
- className) };
+ if ((className instanceof TypeReference)) {
+ if (((TypeReference) className).getName().equals("self")
+ && (goal.getContext() instanceof MethodContext)) {
+ MethodContext methodContext = (MethodContext) goal.getContext();
+ result = methodContext.getInstanceType();
+ return IGoal.NO_GOALS;
+ } else {
+ return new IGoal[] { new ExpressionTypeGoal(goal.getContext(),
+ className) };
+ }
}
result = UnknownType.INSTANCE;
return IGoal.NO_GOALS;
diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
index cdb8389..6d7b477 100644
--- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
+++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/typeinference/evaluators/VariableReferenceEvaluator.java
@@ -29,7 +29,9 @@ import org.eclipse.dltk.ti.goals.GoalEvaluator;
import org.eclipse.dltk.ti.goals.IGoal;
import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.php.internal.core.compiler.ast.nodes.*;
-import org.eclipse.php.internal.core.typeinference.*;
+import org.eclipse.php.internal.core.typeinference.ArrayDeclaration;
+import org.eclipse.php.internal.core.typeinference.Declaration;
+import org.eclipse.php.internal.core.typeinference.PHPTypeInferenceUtils;
import org.eclipse.php.internal.core.typeinference.context.FileContext;
import org.eclipse.php.internal.core.typeinference.context.MethodContext;
import org.eclipse.php.internal.core.typeinference.goals.ArrayDeclarationGoal;
@@ -60,22 +62,7 @@ public class VariableReferenceEvaluator extends GoalEvaluator {
MethodContext methodContext = (MethodContext) context;
IEvaluatedType instanceType = methodContext.getInstanceType();
if (instanceType != null) {
- if (instanceType instanceof PHPClassType
- && methodContext.getType() != null) {
- PHPClassType phpClassType = (PHPClassType) instanceType;
- if (phpClassType.getNamespace() != null) {
- this.results.add(new PHPThisClassType(phpClassType
- .getNamespace(),
- phpClassType.getTypeName(), methodContext
- .getType()));
- } else {
-
- this.results.add(new PHPThisClassType(phpClassType
- .getTypeName(), methodContext.getType()));
- }
- } else {
- this.results.add(instanceType);
- }
+ this.results.add(instanceType);
} else {
this.results.add(new SimpleType(SimpleType.TYPE_NULL));
}