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.JsConditional;
020 import com.google.dart.compiler.backend.js.ast.JsExpression;
021 import com.google.dart.compiler.backend.js.ast.JsLiteral;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression;
025 import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
026 import org.jetbrains.jet.lang.psi.JetSafeQualifiedExpression;
027 import org.jetbrains.k2js.translate.context.TemporaryConstVariable;
028 import org.jetbrains.k2js.translate.context.TranslationContext;
029
030 import static org.jetbrains.k2js.translate.utils.TranslationUtils.isNotNullCheck;
031
032 public enum CallType {
033 SAFE {
034 @NotNull
035 @Override
036 JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
037 @NotNull TranslationContext context) {
038 assert receiver != null;
039
040 TemporaryConstVariable tempVar = context.getOrDeclareTemporaryConstVariable(receiver);
041 return new JsConditional(isNotNullCheck(tempVar.value()), constructor.construct(tempVar.value()), JsLiteral.NULL);
042 }
043 },
044 //TODO: bang qualifier is not implemented in frontend for now
045 // BANG,
046 NORMAL {
047 @NotNull
048 @Override
049 JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
050 @NotNull TranslationContext context) {
051 return constructor.construct(receiver);
052 }
053 };
054
055 @NotNull
056 abstract JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
057 @NotNull TranslationContext context);
058
059 @NotNull
060 public static CallType getCallTypeForQualifiedExpression(@NotNull JetQualifiedExpression expression) {
061 if (expression instanceof JetSafeQualifiedExpression) {
062 return SAFE;
063 }
064 assert expression instanceof JetDotQualifiedExpression;
065 return NORMAL;
066 }
067
068 public interface CallConstructor {
069 @NotNull
070 JsExpression construct(@Nullable JsExpression receiver);
071 }
072
073 }