blob: 39e4aa189189ed4f77769001b4a26e0179df9389 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 xored software, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* xored software, Inc. - initial API and Implementation (Alex Panchenko)
*******************************************************************************/
package org.eclipse.dltk.internal.javascript.ti;
import java.util.Set;
import org.eclipse.dltk.javascript.typeinference.IValueCollection;
import org.eclipse.dltk.javascript.typeinference.IValueReference;
public abstract class ValueCollection implements IValueCollection,
IValueProvider {
private final IValueCollection parent;
private final IValue value;
protected ValueCollection(IValueCollection parent) {
this(parent, new Value());
}
protected ValueCollection(IValueCollection parent, IValue value) {
this.parent = parent;
this.value = value;
}
public IValueCollection getParent() {
return parent;
}
public IValueReference getReturnValue() {
return null;
}
public IValueReference getThis() {
// TODO Auto-generated method stub
return null;
}
public boolean isReference() {
return true;
}
public IValue getValue() {
return value;
}
public IValue createValue() {
return getValue();
}
public final Set<String> getDirectChildren() {
return getDirectChildren(IValue.DEFAULT);
}
public Set<String> getDirectChildren(int options) {
return value.getDirectChildren(options);
}
public Set<String> getDeletedChildren() {
return value.getDeletedChildren();
}
public boolean hasChild(String name) {
IValue value = getValue();
return value != null && value.getChild(name, true) != null;
}
public IValueReference getChild(String name) {
return newChild(name);
}
private ChildReference newChild(String name) {
return new ChildReference(this, name);
}
public IValueReference createChild(String name) {
IValueCollection coll = this;
for (;;) {
if (coll instanceof IValueProvider) {
// XXX WithValueCollection doesn't implement IValueProvider
IValue childValue = ((IValueProvider) coll).getValue()
.getChild(name, false);
if (childValue != null) {
if (coll != this) {
getValue().putChild(name, childValue);
}
return newChild(name);
}
}
coll = coll.getParent();
if (coll == null)
break;
if (coll.isScope())
break;
}
coll = this;
while (!coll.isScope()) {
coll = coll.getParent();
}
IValue childValue = getValue().createChild(name, 0);
if (coll != this) {
getValue().putChild(name, childValue);
}
return newChild(name);
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}