Drop unused stuff in *Utils.

Adapted from https://github.com/develar/kotlin/commit/a9e0a42fb1347fa8e21c86b5a073ef8a7c873da0.
This commit is contained in:
Pavel V. Talanov
2012-08-13 15:51:47 +04:00
parent 40ea266a8a
commit f54c5fad29
5 changed files with 22 additions and 276 deletions
@@ -112,15 +112,6 @@ public final class AnnotationsUtils {
return hasAnnotationOrInsideAnnotatedClass(descriptor, PredefinedAnnotation.LIBRARY);
}
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
for (PredefinedAnnotation annotation : PredefinedAnnotation.values()) {
if (hasAnnotationOrInsideAnnotatedClass(descriptor, annotation)) {
return true;
}
}
return false;
}
public static boolean hasAnnotationOrInsideAnnotatedClass(@NotNull DeclarationDescriptor descriptor,
@NotNull PredefinedAnnotation annotation) {
return hasAnnotationOrInsideAnnotatedClass(descriptor, annotation.getFQName());
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.utils;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.OrderedSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
@@ -30,16 +29,11 @@ import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.types.JetType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.lang.resolve.BindingContext.INDEXED_LVALUE_GET;
import static org.jetbrains.jet.lang.resolve.BindingContext.INDEXED_LVALUE_SET;
import static org.jetbrains.k2js.translate.utils.ErrorReportingUtils.message;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getContainedDescriptorsWhichAreNotPredefined;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getNamespaceDescriptorHierarchy;
/**
* @author Pavel Talanov
@@ -69,15 +63,6 @@ public final class BindingUtils {
return getDescriptorForExpression(context, declaration, ClassDescriptor.class);
}
@NotNull
public static NamespaceDescriptor getNamespaceDescriptor(@NotNull BindingContext context,
@NotNull JetFile declaration) {
NamespaceDescriptor namespaceDescriptor =
context.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, JetPsiUtil.getFQName(declaration));
assert namespaceDescriptor != null : message(declaration, "File should have a namespace descriptor");
return namespaceDescriptor;
}
@NotNull
public static FunctionDescriptor getFunctionDescriptor(@NotNull BindingContext context,
@NotNull JetDeclarationWithBody declaration) {
@@ -90,14 +75,6 @@ public final class BindingUtils {
return getDescriptorForExpression(context, declaration, PropertyDescriptor.class);
}
@NotNull
public static JetClass getClassForDescriptor(@NotNull BindingContext context,
@NotNull ClassDescriptor descriptor) {
PsiElement result = BindingContextUtils.classDescriptorToDeclaration(context, descriptor);
assert result instanceof JetClass : message(context, descriptor, "JetClass not found for " + descriptor);
return (JetClass) result;
}
@NotNull
public static JetFunction getFunctionForDescriptor(@NotNull BindingContext context,
@NotNull SimpleFunctionDescriptor descriptor) {
@@ -107,34 +84,6 @@ public final class BindingUtils {
return (JetFunction) result;
}
@NotNull
public static List<JetDeclaration> getDeclarationsForNamespace(@NotNull BindingContext bindingContext,
@NotNull NamespaceDescriptor namespace) {
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespace, bindingContext)) {
if (descriptor instanceof NamespaceDescriptor) {
continue;
}
JetDeclaration declaration = getDeclarationForDescriptor(bindingContext, descriptor);
if (declaration != null) {
declarations.add(declaration);
}
}
return declarations;
}
@Nullable
private static JetDeclaration getDeclarationForDescriptor(@NotNull BindingContext context,
@NotNull DeclarationDescriptor descriptor) {
PsiElement result = BindingContextUtils.descriptorToDeclaration(context, descriptor);
if (result == null) {
//TODO: never get there
return null;
}
assert result instanceof JetDeclaration : message(context, descriptor, "Descriptor should correspond to an element");
return (JetDeclaration) result;
}
@NotNull
private static JetParameter getParameterForDescriptor(@NotNull BindingContext context,
@NotNull ValueParameterDescriptor descriptor) {
@@ -176,16 +125,6 @@ public final class BindingUtils {
return context.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
}
@Nullable
public static JetProperty getPropertyForDescriptor(@NotNull BindingContext context,
@NotNull PropertyDescriptor property) {
PsiElement result = BindingContextUtils.descriptorToDeclaration(context, property);
if (!(result instanceof JetProperty)) {
return null;
}
return (JetProperty) result;
}
@NotNull
public static DeclarationDescriptor getDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull JetReferenceExpression reference) {
@@ -327,20 +266,6 @@ public final class BindingUtils {
return propertyDescriptor;
}
@NotNull
public static Set<NamespaceDescriptor> getAllNonNativeNamespaceDescriptors(@NotNull BindingContext context,
@NotNull Collection<JetFile> files) {
Set<NamespaceDescriptor> descriptorSet = new OrderedSet<NamespaceDescriptor>();
for (JetFile file : files) {
//TODO: can't be
NamespaceDescriptor namespaceDescriptor = getNamespaceDescriptor(context, file);
if (!AnnotationsUtils.isPredefinedObject(namespaceDescriptor)) {
descriptorSet.addAll(getNamespaceDescriptorHierarchy(namespaceDescriptor));
}
}
return descriptorSet;
}
@NotNull
public static JetType getTypeForExpression(@NotNull BindingContext context,
@NotNull JetExpression expression) {
@@ -16,7 +16,6 @@
package org.jetbrains.k2js.translate.utils;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
@@ -28,7 +27,6 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
@@ -52,18 +50,12 @@ public final class JsAstUtils {
private JsAstUtils() {
}
@NotNull
public static JsPropertyInitializer newNamedMethod(@NotNull JsName name, @NotNull JsFunction function) {
JsNameRef methodName = name.makeRef();
return new JsPropertyInitializer(methodName, function);
}
@NotNull
public static JsStatement convertToStatement(@NotNull JsNode jsNode) {
assert (jsNode instanceof JsExpression) || (jsNode instanceof JsStatement)
: "Unexpected node of type: " + jsNode.getClass().toString();
if (jsNode instanceof JsExpression) {
return new JsExprStmt((JsExpression) jsNode);
return ((JsExpression) jsNode).makeStmt();
}
return (JsStatement) jsNode;
}
@@ -73,8 +65,7 @@ public final class JsAstUtils {
if (jsNode instanceof JsBlock) {
return (JsBlock) jsNode;
}
JsStatement jsStatement = convertToStatement(jsNode);
return new JsBlock(jsStatement);
return new JsBlock(convertToStatement(jsNode));
}
@NotNull
@@ -83,19 +74,6 @@ public final class JsAstUtils {
return (JsExpression) jsNode;
}
public static JsNameRef thisQualifiedReference(@NotNull JsName name) {
JsNameRef result = name.makeRef();
result.setQualifier(JsLiteral.THIS);
return result;
}
@NotNull
public static JsBlock newBlock(List<JsStatement> statements) {
JsBlock result = new JsBlock();
setStatements(result, statements);
return result;
}
@NotNull
public static JsPrefixOperation negated(@NotNull JsExpression expression) {
return new JsPrefixOperation(JsUnaryOperator.NOT, expression);
@@ -130,12 +108,6 @@ public final class JsAstUtils {
}
}
public static JsNameRef qualified(@NotNull JsName selector, @Nullable JsExpression qualifier) {
JsNameRef reference = selector.makeRef();
setQualifier(reference, qualifier);
return reference;
}
@NotNull
public static JsBinaryOperation equality(@NotNull JsExpression arg1, @NotNull JsExpression arg2) {
return new JsBinaryOperation(JsBinaryOperator.REF_EQ, arg1, arg2);
@@ -186,28 +158,15 @@ public final class JsAstUtils {
return result;
}
@NotNull
public static JsObjectLiteral newObjectLiteral(@NotNull List<JsPropertyInitializer> propertyList) {
JsObjectLiteral jsObjectLiteral = new JsObjectLiteral();
jsObjectLiteral.getPropertyInitializers().addAll(propertyList);
return jsObjectLiteral;
}
@NotNull
public static JsVars newVar(@NotNull JsName name, @Nullable JsExpression expr) {
return new JsVars(new JsVars.JsVar(name, expr));
}
public static void addVarDeclaration(@NotNull JsBlock block, @NotNull JsVars vars) {
LinkedList<JsStatement> statementLinkedList = Lists.newLinkedList(block.getStatements());
statementLinkedList.offer(vars);
setStatements(block, statementLinkedList);
}
private static void setStatements(@NotNull JsBlock block, @NotNull List<JsStatement> statements) {
List<JsStatement> statementList = block.getStatements();
statementList.clear();
statementList.addAll(statements);
public static void setArguments(@NotNull JsInvocation invocation, @NotNull List<JsExpression> newArgs) {
List<JsExpression> arguments = invocation.getArguments();
assert arguments.isEmpty() : "Arguments already set.";
arguments.addAll(newArgs);
}
public static void setArguments(@NotNull HasArguments invocation, @NotNull List<JsExpression> newArgs) {
@@ -226,20 +185,6 @@ public final class JsAstUtils {
parameters.addAll(newParams);
}
public static void setParameters(@NotNull JsFunction function, JsParameter... arguments) {
setParameters(function, Arrays.asList(arguments));
}
@NotNull
public static JsInvocation newInvocation(@NotNull JsExpression target, List<JsExpression> params) {
JsInvocation invoke = new JsInvocation();
invoke.setQualifier(target);
for (JsExpression expr : params) {
invoke.getArguments().add(expr);
}
return invoke;
}
@NotNull
public static JsExpression newSequence(@NotNull List<JsExpression> expressions) {
assert !expressions.isEmpty();
@@ -255,9 +200,7 @@ public final class JsAstUtils {
@NotNull
public static JsFunction createFunctionWithEmptyBody(@NotNull JsScope parent) {
JsFunction correspondingFunction = new JsFunction(parent);
correspondingFunction.setBody(new JsBlock());
return correspondingFunction;
return new JsFunction(parent, new JsBlock());
}
@NotNull
@@ -330,14 +273,6 @@ public final class JsAstUtils {
return dataDescriptor;
}
@NotNull
public static JsInvocation encloseFunction(@NotNull JsFunction function) {
JsInvocation blockFunctionInvocation = new JsInvocation();
blockFunctionInvocation.setQualifier(EMPTY_REF);
blockFunctionInvocation.getArguments().add(function);
return blockFunctionInvocation;
}
@NotNull
public static JsFunction createPackage(@NotNull List<JsStatement> to, @NotNull JsScope scope) {
JsFunction packageBlockFunction = createFunctionWithEmptyBody(scope);
@@ -16,22 +16,14 @@
package org.jetbrains.k2js.translate.utils;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.k2js.config.LibrarySourcesConfig;
import java.util.List;
import java.util.Set;
@@ -131,31 +123,6 @@ public final class JsDescriptorUtils {
return null;
}
@Nullable
public static NamespaceDescriptor getContainingNamespace(@NotNull DeclarationDescriptor descriptor) {
return DescriptorUtils.getParentOfType(descriptor, NamespaceDescriptor.class);
}
public static boolean isStandardDeclaration(@NotNull DeclarationDescriptor descriptor) {
NamespaceDescriptor namespace = getContainingNamespace(descriptor);
if (namespace == null) {
return false;
}
return namespace.equals(JetStandardLibrary.getInstance().getLibraryScope().getContainingDeclaration());
}
@NotNull
public static List<NamespaceDescriptor> getNestedNamespaces(@NotNull NamespaceDescriptor namespaceDescriptor,
@NotNull BindingContext context) {
List<NamespaceDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor, context)) {
if (descriptor instanceof NamespaceDescriptor) {
result.add((NamespaceDescriptor) descriptor);
}
}
return result;
}
@Nullable
public static FunctionDescriptor getOverriddenDescriptor(@NotNull FunctionDescriptor functionDescriptor) {
Set<? extends FunctionDescriptor> overriddenDescriptors = functionDescriptor.getOverriddenDescriptors();
@@ -168,63 +135,6 @@ public final class JsDescriptorUtils {
}
}
@NotNull
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace,
@NotNull BindingContext context) {
List<DeclarationDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
if (!AnnotationsUtils.isPredefinedObject(descriptor)) {
// namespace may be defined in multiple files
if (!(descriptor instanceof NamespaceDescriptor)) {
PsiElement psiElement = BindingContextUtils.descriptorToDeclaration(context, descriptor);
if (psiElement != null) {
PsiFile file = psiElement.getContainingFile();
if (file.getUserData(LibrarySourcesConfig.EXTERNAL_MODULE_NAME) != null) {
continue;
}
}
}
result.add(descriptor);
}
}
return result;
}
//TODO: at the moment this check is very ineffective
public static boolean isNamespaceEmpty(@NotNull NamespaceDescriptor namespace, @NotNull BindingContext context) {
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace, context);
for (DeclarationDescriptor descriptor : containedDescriptors) {
if (descriptor instanceof NamespaceDescriptor) {
if (!isNamespaceEmpty((NamespaceDescriptor) descriptor, context)) {
return false;
}
}
else {
return false;
}
}
return true;
}
@NotNull
public static List<NamespaceDescriptor> getNamespaceDescriptorHierarchy(@NotNull NamespaceDescriptor namespaceDescriptor) {
List<NamespaceDescriptor> result = Lists.newArrayList(namespaceDescriptor);
NamespaceDescriptor current = namespaceDescriptor;
while (!(current.getContainingDeclaration() instanceof ModuleDescriptor)) {
result.add(current);
if (current.getContainingDeclaration() instanceof NamespaceDescriptor) {
current = (NamespaceDescriptor) current.getContainingDeclaration();
//noinspection ConstantConditions
assert current != null;
}
else {
break;
}
}
return result;
}
private static boolean isDefaultAccessor(@Nullable PropertyAccessorDescriptor accessorDescriptor) {
return accessorDescriptor == null || accessorDescriptor.isDefault();
}
@@ -244,7 +154,7 @@ public final class JsDescriptorUtils {
return ((ExtensionReceiver) receiverArgument).getDeclarationDescriptor();
}
if (receiverArgument instanceof ClassReceiver) {
return ((ClassReceiver)receiverArgument).getDeclarationDescriptor();
return ((ClassReceiver) receiverArgument).getDeclarationDescriptor();
}
throw new IllegalStateException("Unexpected receiver of type " + receiverArgument.getClass());
}
@@ -17,13 +17,13 @@
package org.jetbrains.k2js.translate.utils;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyGetterDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation;
@@ -33,16 +33,11 @@ import java.util.List;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptorForOperationExpression;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getDeclarationDescriptorForReceiver;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getExpectedReceiverDescriptor;
/**
* @author Pavel Talanov
*/
public final class TranslationUtils {
private static final JsNameRef UNDEFINED_LITERAL = AstUtil.newQualifiedNameRef("undefined");
private TranslationUtils() {
}
@@ -129,10 +124,18 @@ public final class TranslationUtils {
}
@NotNull
public static JsNameRef getQualifiedReference(@NotNull TranslationContext context, @NotNull DeclarationDescriptor descriptor) {
return new JsNameRef(context.getNameForDescriptor(descriptor), context.getQualifierForDescriptor(descriptor));
public static JsNameRef getQualifiedReference(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor descriptor) {
JsName name = context.getNameForDescriptor(descriptor);
JsNameRef reference = name.makeRef();
JsNameRef qualifier = context.getQualifierForDescriptor(descriptor);
if (qualifier != null) {
setQualifier(reference, qualifier);
}
return reference;
}
@NotNull
public static List<JsExpression> translateExpressionList(@NotNull TranslationContext context,
@NotNull List<JetExpression> expressions) {
@@ -173,22 +176,4 @@ public final class TranslationUtils {
return false;
}
@Nullable
public static JsExpression resolveThisObjectForResolvedCall(@NotNull ResolvedCall<?> call,
@NotNull TranslationContext context) {
ReceiverDescriptor thisObject = call.getThisObject();
if (!thisObject.exists()) {
return null;
}
DeclarationDescriptor expectedThisDescriptor = getDeclarationDescriptorForReceiver(thisObject);
return getThisObject(context, expectedThisDescriptor);
}
public static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements,
String moduleId) {
statements.add(new JsInvocation(context.namer().kotlin("defineModule"),
context.program().getStringLiteral(moduleId),
context.scope().declareName("_").makeRef()).makeStmt());
}
}