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