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.inline;
018
019 import com.google.common.collect.Iterables;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.org.objectweb.asm.Type;
022
023 import java.util.ArrayList;
024 import java.util.Iterator;
025 import java.util.List;
026
027 //All parameters with gaps
028 public class Parameters implements Iterable<ParameterInfo> {
029 private final List<ParameterInfo> real;
030 private final List<CapturedParamInfo> captured;
031
032 public Parameters(List<ParameterInfo> real, List<CapturedParamInfo> captured) {
033 this.real = real;
034 this.captured = captured;
035 }
036
037 public List<ParameterInfo> getReal() {
038 return real;
039 }
040
041 public List<CapturedParamInfo> getCaptured() {
042 return captured;
043 }
044
045 public int totalSize() {
046 return real.size() + captured.size();
047 }
048
049 public ParameterInfo get(int index) {
050 if (index < real.size()) {
051 return real.get(index);
052 }
053 return captured.get(index - real.size());
054 }
055
056 @NotNull
057 @Override
058 public Iterator<ParameterInfo> iterator() {
059 return Iterables.concat(real, captured).iterator();
060 }
061
062 public static List<CapturedParamInfo> shiftAndAddStubs(List<CapturedParamInfo> capturedParams, int realSize) {
063 List<CapturedParamInfo> result = new ArrayList<CapturedParamInfo>();
064 for (CapturedParamInfo capturedParamInfo : capturedParams) {
065 CapturedParamInfo newInfo = capturedParamInfo.newIndex(result.size() + realSize);
066 result.add(newInfo);
067 if (capturedParamInfo.getType().getSize() == 2) {
068 result.add(CapturedParamInfo.STUB);
069 }
070 }
071 return result;
072 }
073
074 public static List<ParameterInfo> addStubs(List<ParameterInfo> params) {
075 List<ParameterInfo> result = new ArrayList<ParameterInfo>();
076 for (ParameterInfo newInfo : params) {
077 result.add(newInfo);
078 if (newInfo.getType().getSize() == 2) {
079 result.add(ParameterInfo.STUB);
080 }
081 }
082 return result;
083 }
084
085 public ArrayList<Type> getCapturedTypes() {
086 ArrayList<Type> result = new ArrayList<Type>();
087 for (CapturedParamInfo info : captured) {
088 if(info != CapturedParamInfo.STUB) {
089 result.add(info.getType());
090 }
091 }
092 return result;
093 }
094 }