001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.k2js.translate.reference;
018
019 import com.google.dart.compiler.backend.js.ast.JsExpression;
020 import com.google.dart.compiler.backend.js.ast.JsName;
021 import com.google.dart.compiler.backend.js.ast.JsNameRef;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
025 import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
026 import org.jetbrains.k2js.translate.context.TranslationContext;
027
028 import static org.jetbrains.jet.lang.psi.JetPsiUtil.isBackingFieldReference;
029
030 public final class ReferenceTranslator {
031
032 private ReferenceTranslator() {
033 }
034
035 @NotNull
036 public static JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression,
037 @NotNull TranslationContext context) {
038 return getAccessTranslator(expression, context).translateAsGet();
039 }
040
041 @NotNull
042 public static JsExpression translateAsFQReference(@NotNull DeclarationDescriptor referencedDescriptor,
043 @NotNull TranslationContext context) {
044 JsExpression qualifier = context.getQualifierForDescriptor(referencedDescriptor);
045 if (qualifier == null) {
046 return translateAsLocalNameReference(referencedDescriptor, context);
047 }
048 JsName referencedName = context.getNameForDescriptor(referencedDescriptor);
049 return new JsNameRef(referencedName, qualifier);
050 }
051
052 @NotNull
053 public static JsExpression translateAsLocalNameReference(@NotNull DeclarationDescriptor referencedDescriptor,
054 @NotNull TranslationContext context) {
055 //Preconditions.checkNotNull(referencedDescriptor, "No referencedDescriptor available");
056 return context.getNameForDescriptor(referencedDescriptor).makeRef();
057 }
058
059 @NotNull
060 public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
061 @NotNull TranslationContext context) {
062 return getAccessTranslator(referenceExpression, null, context);
063 }
064
065 @NotNull
066 public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
067 @Nullable JsExpression receiver,
068 @NotNull TranslationContext context) {
069 if (isBackingFieldReference(referenceExpression)) {
070 return BackingFieldAccessTranslator.newInstance(referenceExpression, context);
071 }
072 if (PropertyAccessTranslator.canBePropertyAccess(referenceExpression, context)) {
073 return PropertyAccessTranslator.newInstance(referenceExpression, receiver, CallType.NORMAL, context);
074 }
075 return ReferenceAccessTranslator.newInstance(referenceExpression, context);
076 }
077 }