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.descriptors.PropertyDescriptor;
023import org.jetbrains.jet.lang.psi.JetPsiUtil;
024import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
025import org.jetbrains.k2js.translate.context.TemporaryVariable;
026import org.jetbrains.k2js.translate.context.TranslationContext;
027import org.jetbrains.k2js.translate.general.AbstractTranslator;
028
029import java.util.Collections;
030import java.util.List;
031
032import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
033import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
034import static org.jetbrains.k2js.translate.utils.TranslationUtils.backingFieldReference;
035
036public final class BackingFieldAccessTranslator extends AbstractTranslator implements CachedAccessTranslator {
037
038    @NotNull
039    private final PropertyDescriptor descriptor;
040
041    /*package*/
042    static BackingFieldAccessTranslator newInstance(@NotNull JetSimpleNameExpression expression,
043                                                    @NotNull TranslationContext context) {
044        assert JetPsiUtil.isBackingFieldReference(expression);
045        DeclarationDescriptor referencedProperty = getDescriptorForReferenceExpression(context.bindingContext(), expression);
046        assert referencedProperty instanceof PropertyDescriptor;
047        return new BackingFieldAccessTranslator((PropertyDescriptor) referencedProperty, context);
048    }
049
050    private BackingFieldAccessTranslator(@NotNull PropertyDescriptor descriptor, @NotNull TranslationContext context) {
051        super(context);
052        this.descriptor = descriptor;
053    }
054
055    @NotNull
056    @Override
057    public JsExpression translateAsGet() {
058        return backingFieldReference(context(), descriptor);
059    }
060
061    @NotNull
062    @Override
063    public JsExpression translateAsSet(@NotNull JsExpression setTo) {
064        return assignmentToBackingField(context(), descriptor, setTo);
065    }
066
067    @NotNull
068    @Override
069    public CachedAccessTranslator getCached() {
070        return this;
071    }
072
073    @NotNull
074    @Override
075    public List<TemporaryVariable> declaredTemporaries() {
076        return Collections.emptyList();
077    }
078}