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
017package org.jetbrains.k2js.translate.reference;
018
019import com.google.dart.compiler.backend.js.ast.JsExpression;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
023import org.jetbrains.k2js.translate.context.TemporaryVariable;
024import org.jetbrains.k2js.translate.context.TranslationContext;
025import org.jetbrains.k2js.translate.general.AbstractTranslator;
026import org.jetbrains.k2js.translate.utils.JsAstUtils;
027
028import java.util.Collections;
029import java.util.List;
030
031import static org.jetbrains.k2js.translate.reference.ReferenceTranslator.translateAsLocalNameReference;
032import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
033
034public final class ReferenceAccessTranslator extends AbstractTranslator implements CachedAccessTranslator {
035
036    @NotNull
037    /*package*/ static ReferenceAccessTranslator newInstance(@NotNull JetSimpleNameExpression expression,
038                                                             @NotNull TranslationContext context) {
039        DeclarationDescriptor referenceDescriptor = getDescriptorForReferenceExpression(context.bindingContext(), expression);
040        assert referenceDescriptor != null : "JetSimpleName expression must reference a descriptor " + expression.getText();
041        return new ReferenceAccessTranslator(referenceDescriptor, context);
042    }
043
044    @NotNull
045    private final JsExpression reference;
046
047    private ReferenceAccessTranslator(@NotNull DeclarationDescriptor descriptor, @NotNull TranslationContext context) {
048        super(context);
049        this.reference = translateAsLocalNameReference(descriptor, context());
050    }
051
052    @Override
053    @NotNull
054    public JsExpression translateAsGet() {
055        return reference;
056    }
057
058    @Override
059    @NotNull
060    public JsExpression translateAsSet(@NotNull JsExpression toSetTo) {
061        return JsAstUtils.assignment(reference, toSetTo);
062    }
063
064    @NotNull
065    @Override
066    public CachedAccessTranslator getCached() {
067        return this;
068    }
069
070    @NotNull
071    @Override
072    public List<TemporaryVariable> declaredTemporaries() {
073        return Collections.emptyList();
074    }
075}