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.codegen.signature;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.load.kotlin.JvmDescriptorTypeWriter;
022 import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature;
023 import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind;
024 import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature;
025 import org.jetbrains.kotlin.types.Variance;
026 import org.jetbrains.org.objectweb.asm.Type;
027 import org.jetbrains.org.objectweb.asm.commons.Method;
028
029 import java.util.ArrayList;
030 import java.util.List;
031
032 public class JvmSignatureWriter extends JvmDescriptorTypeWriter<Type> {
033
034 private final List<JvmMethodParameterSignature> kotlinParameterTypes = new ArrayList<JvmMethodParameterSignature>();
035
036 private Type jvmReturnType;
037
038 private JvmMethodParameterKind currentParameterKind;
039
040 private int currentSignatureSize = 0;
041
042 public JvmSignatureWriter() {
043 super(AsmTypeFactory.INSTANCE);
044 }
045
046 @Override
047 public void writeClass(@NotNull Type objectType) {
048 writeClassBegin(objectType);
049 writeClassEnd();
050 }
051
052 public void writeAsmType(@NotNull Type asmType) {
053 switch (asmType.getSort()) {
054 case Type.OBJECT:
055 writeClassBegin(asmType);
056 writeClassEnd();
057 return;
058 case Type.ARRAY:
059 writeArrayType();
060 writeAsmType(asmType.getElementType());
061 writeArrayEnd();
062 return;
063 default:
064 writeJvmTypeAsIs(asmType);
065 }
066 }
067
068 public void writeClassBegin(Type asmType) {
069 writeJvmTypeAsIs(asmType);
070 }
071
072 public void writeOuterClassBegin(Type resultingAsmType, String outerInternalName) {
073 writeJvmTypeAsIs(resultingAsmType);
074 }
075
076 public void writeInnerClass(String name) {
077 }
078
079 public void writeClassEnd() {
080 }
081
082 public void writeTypeArgument(@NotNull Variance projectionKind) {
083 }
084
085 public void writeUnboundedWildcard() {
086 }
087
088 public void writeTypeArgumentEnd() {
089 }
090
091 public void writeFormalTypeParameter(String name) {
092 }
093
094 public void writeClassBound() {
095 }
096
097 public void writeClassBoundEnd() {
098 }
099
100 public void writeInterfaceBound() {
101 }
102
103 public void writeInterfaceBoundEnd() {
104 }
105
106 public void writeParametersStart() {
107 // hacks
108 clearCurrentType();
109 }
110
111 public void writeParameterType(JvmMethodParameterKind parameterKind) {
112 currentParameterKind = parameterKind;
113 }
114
115 public void writeParameterTypeEnd() {
116 //noinspection ConstantConditions
117 kotlinParameterTypes.add(new JvmMethodParameterSignature(getJvmCurrentType(), currentParameterKind));
118 currentSignatureSize += getJvmCurrentType().getSize();
119
120 currentParameterKind = null;
121 clearCurrentType();
122 }
123
124 public void writeReturnType() {
125 }
126
127 public void writeReturnTypeEnd() {
128 jvmReturnType = getJvmCurrentType();
129 clearCurrentType();
130 }
131
132 public void writeSuperclass() {
133 }
134
135 public void writeSuperclassEnd() {
136 }
137
138 public void writeInterface() {
139 }
140
141 public void writeInterfaceEnd() {
142 }
143
144 @Nullable
145 public String makeJavaGenericSignature() {
146 return null;
147 }
148
149 @NotNull
150 public JvmMethodGenericSignature makeJvmMethodSignature(@NotNull String name) {
151 List<Type> types = new ArrayList<Type>(kotlinParameterTypes.size());
152 for (JvmMethodParameterSignature parameter : kotlinParameterTypes) {
153 types.add(parameter.getAsmType());
154 }
155 Method asmMethod = new Method(name, jvmReturnType, types.toArray(new Type[types.size()]));
156 return new JvmMethodGenericSignature(asmMethod, kotlinParameterTypes, makeJavaGenericSignature());
157 }
158
159 public int getCurrentSignatureSize() {
160 return currentSignatureSize;
161 }
162
163 public boolean skipGenericSignature() {
164 return true;
165 }
166
167 @Override
168 public String toString() {
169 return "empty";
170 }
171 }
172