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.jet.lang.types.lang;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
022    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
023    import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
024    import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
025    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
026    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
027    import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
028    import org.jetbrains.jet.lang.resolve.constants.EnumValue;
029    
030    public class InlineUtil {
031    
032        public static boolean DEFAULT_INLINE_FLAG = true;
033    
034        public static boolean DEFAULT_INLINE_FLAG_FOR_TEST = true;
035    
036        public static boolean DEFAULT_INLINE_FLAG_FOR_TOOLWINDOW = true;
037    
038        public static boolean DEFAULT_INLINE_FLAG_FOR_STUB = false; /*always false*/
039    
040        public static boolean hasNoinlineAnnotation(@NotNull CallableDescriptor valueParameterDescriptor) {
041            KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
042            return KotlinBuiltIns.containsAnnotation(valueParameterDescriptor, builtIns.getNoinlineClassAnnotation());
043        }
044    
045        @NotNull
046        public static InlineStrategy getInlineType(@Nullable Annotations annotations) {
047            KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
048            ClassDescriptor annotationClass = builtIns.getInlineClassAnnotation();
049            AnnotationDescriptor annotation = getAnnotation(annotations, annotationClass);
050            if (annotation != null) {
051                ValueParameterDescriptor parameterDescriptor = annotationClass.getConstructors().iterator().next().getValueParameters().get(0);
052                CompileTimeConstant<?> argument = annotation.getValueArgument(parameterDescriptor);
053                if (argument == null) {
054                    //default parameter
055                    return InlineStrategy.AS_FUNCTION;
056                }
057                else {
058                    assert argument instanceof EnumValue : "Inline annotation parameter should be inline entry but was: " + argument + "!";
059                    ClassDescriptor value = ((EnumValue) argument).getValue();
060                    assert value != null : "Value for enum value should be not null " + argument;
061                    String name = value.getName().asString();
062                    return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION;
063                }
064            }
065            else {
066                return InlineStrategy.NOT_INLINE;
067            }
068        }
069    
070        @Nullable
071        private static AnnotationDescriptor getAnnotation(@Nullable Annotations annotations, @NotNull ClassDescriptor annotationClass) {
072            if (annotations != null) {
073                for (AnnotationDescriptor annotation : annotations) {
074                    if (annotationClass.equals(annotation.getType().getConstructor().getDeclarationDescriptor())) {
075                        return annotation;
076                    }
077                }
078            }
079            return null;
080        }
081    
082    
083    }
084