Temporarily prohibit non-local returns for some stdlib functions

Make InlineUtil work with FQ names, not descriptors

 #KT-5496 Fixed
 #KT-5497 Fixed
This commit is contained in:
Alexander Udalov
2014-07-18 20:03:57 +04:00
parent 540b87a1dc
commit 70adb0f4e2
8 changed files with 96 additions and 77 deletions
@@ -1,49 +0,0 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.types.lang
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant
import org.jetbrains.jet.lang.descriptors.annotations.Annotations
public object AnnotationUtil {
public fun getAnnotationSingleArgument(descriptor: DeclarationDescriptor, annotationClass: ClassDescriptor) : CompileTimeConstant<out Any?>? {
val annotation = getAnnotation(descriptor.getAnnotations(), annotationClass)
if (annotation != null) {
val parameterDescriptor = annotationClass.getConstructors().elementAt(0).getValueParameters().first
if (parameterDescriptor != null) {
return annotation.getValueArgument(parameterDescriptor)
}
}
return null
}
public fun getAnnotation(annotations: Annotations, annotationClass: ClassDescriptor): AnnotationDescriptor? {
for (annotation in annotations) {
if (annotationClass == annotation.getType().getConstructor().getDeclarationDescriptor()) {
return annotation
}
}
return null
}
}
@@ -16,12 +16,16 @@
package org.jetbrains.jet.lang.types.lang;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.constants.ArrayValue;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.constants.EnumValue;
@@ -31,29 +35,23 @@ import java.util.List;
public class InlineUtil {
public static boolean hasNoinlineAnnotation(@NotNull CallableDescriptor valueParameterDescriptor) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
return KotlinBuiltIns.containsAnnotation(valueParameterDescriptor, builtIns.getNoinlineClassAnnotation());
return KotlinBuiltIns.containsAnnotation(valueParameterDescriptor, KotlinBuiltIns.getInstance().getNoinlineClassAnnotation());
}
@NotNull
public static InlineStrategy getInlineType(@NotNull DeclarationDescriptor descriptor) {
ClassDescriptor annotationClass = KotlinBuiltIns.getInstance().getInlineClassAnnotation();
AnnotationDescriptor annotation = AnnotationUtil.instance$.getAnnotation(descriptor.getAnnotations(), annotationClass);
if (annotation != null) {
CompileTimeConstant<?> argument = AnnotationUtil.instance$.getAnnotationSingleArgument(descriptor, annotationClass);
if (argument == null) {
//default parameter
return InlineStrategy.AS_FUNCTION;
}
else {
assert argument instanceof EnumValue : "Inline annotation parameter should be inline entry but was: " + argument + "!";
String name = ((EnumValue) argument).getValue().getName().asString();
return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION;
}
}
else {
ClassDescriptor inlineAnnotation = KotlinBuiltIns.getInstance().getInlineClassAnnotation();
AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(DescriptorUtils.getFqNameSafe(inlineAnnotation));
if (annotation == null) {
return InlineStrategy.NOT_INLINE;
}
CompileTimeConstant<?> argument = getAnnotationSingleArgument(descriptor, inlineAnnotation);
if (argument == null) {
return InlineStrategy.AS_FUNCTION;
}
assert argument instanceof EnumValue : "Inline annotation parameter should be enum entry but was: " + argument;
String name = ((EnumValue) argument).getValue().getName().asString();
return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION;
}
public static boolean hasOnlyLocalContinueAndBreak(@NotNull ValueParameterDescriptor descriptor) {
@@ -66,8 +64,7 @@ public class InlineUtil {
private static boolean hasInlineOption(@NotNull ValueParameterDescriptor descriptor, @NotNull InlineOption option) {
CompileTimeConstant<?> argument =
AnnotationUtil.instance$.getAnnotationSingleArgument(descriptor, KotlinBuiltIns.getInstance()
.getInlineOptionsClassAnnotation());
getAnnotationSingleArgument(descriptor, KotlinBuiltIns.getInstance().getInlineOptionsClassAnnotation());
if (argument instanceof ArrayValue) {
List<CompileTimeConstant<?>> values = ((ArrayValue) argument).getValue();
@@ -83,5 +80,16 @@ public class InlineUtil {
return false;
}
}
@Nullable
private static CompileTimeConstant<?> getAnnotationSingleArgument(
@NotNull Annotated annotated,
@NotNull ClassDescriptor annotationClass
) {
AnnotationDescriptor annotation = annotated.getAnnotations().findAnnotation(DescriptorUtils.getFqNameSafe(annotationClass));
if (annotation != null) {
return KotlinPackage.firstOrNull(annotation.getAllValueArguments().values());
}
return null;
}
}