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 org.jetbrains.annotations.Nullable; 020 import org.jetbrains.asm4.Type; 021 022 import java.util.ArrayList; 023 import java.util.Collections; 024 import java.util.List; 025 026 public class ParametersBuilder { 027 028 private final List<ParameterInfo> params = new ArrayList<ParameterInfo>(); 029 private final List<CapturedParamInfo> capturedParams = new ArrayList<CapturedParamInfo>(); 030 031 private int nextIndex = 0; 032 private int nextCaptured = 0; 033 034 public static ParametersBuilder newBuilder() { 035 return new ParametersBuilder(); 036 } 037 038 public ParameterInfo addThis(Type type, boolean skipped) { 039 ParameterInfo info = new ParameterInfo(type, skipped, nextIndex, -1); 040 addParameter(info); 041 return info; 042 } 043 044 public ParametersBuilder addNextParameter(Type type, boolean skipped, @Nullable ParameterInfo original) { 045 addParameter(new ParameterInfo(type, skipped, nextIndex, original != null ? original.getIndex() : -1)); 046 return this; 047 } 048 049 public CapturedParamInfo addCapturedParam(String fieldName, Type type, boolean skipped, @Nullable ParameterInfo original) { 050 return addCapturedParameter(new CapturedParamInfo(fieldName, type, skipped, nextCaptured, 051 original != null ? original.getIndex() : -1)); 052 } 053 054 private void addParameter(ParameterInfo info) { 055 params.add(info); 056 nextIndex += info.getType().getSize(); 057 } 058 059 private CapturedParamInfo addCapturedParameter(CapturedParamInfo info) { 060 capturedParams.add(info); 061 nextCaptured += info.getType().getSize(); 062 return info; 063 } 064 065 public List<ParameterInfo> build() { 066 return Collections.unmodifiableList(params); 067 } 068 069 public List<CapturedParamInfo> buildCaptured() { 070 return Collections.unmodifiableList(capturedParams); 071 } 072 073 public List<ParameterInfo> buildWithStubs() { 074 return Parameters.addStubs(build()); 075 } 076 077 public List<CapturedParamInfo> buildCapturedWithStubs() { 078 return Parameters.shiftAndAddStubs(buildCaptured(), nextIndex); 079 } 080 081 public Parameters buildParameters() { 082 return new Parameters(buildWithStubs(), buildCapturedWithStubs()); 083 } 084 }