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