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.jet.codegen.context.CodegenContext;
020    import org.jetbrains.jet.codegen.state.GenerationState;
021    import org.jetbrains.jet.lang.psi.Call;
022    
023    import java.util.Collections;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    public class InliningContext {
029    
030        public final Map<Integer, LambdaInfo> expressionMap;
031    
032        public final List<InvokeCall> invokeCalls;
033    
034        public final List<ConstructorInvocation> constructorInvocation;
035    
036        public final VarRemapper remapper;
037    
038        public final GenerationState state;
039    
040        public final NameGenerator nameGenerator;
041    
042        public final CodegenContext startContext;
043    
044        public final Call call;
045    
046        public final Map<String, String> typeMapping;
047    
048        public final boolean isInliningLambda;
049    
050        public final boolean classRegeneration;
051    
052        public InliningContext(
053                Map<Integer, LambdaInfo> map,
054                List<InvokeCall> accesses,
055                List<ConstructorInvocation> invocation,
056                VarRemapper remapper,
057                GenerationState state,
058                NameGenerator nameGenerator,
059                CodegenContext startContext,
060                Call call,
061                Map<String, String> typeMapping,
062                boolean isInliningLambda,
063                boolean classRegeneration
064        ) {
065            expressionMap = map;
066            invokeCalls = accesses;
067            constructorInvocation = invocation;
068            this.remapper = remapper;
069            this.state = state;
070            this.nameGenerator = nameGenerator;
071            this.startContext = startContext;
072            this.call = call;
073            this.typeMapping = typeMapping;
074            this.isInliningLambda = isInliningLambda;
075            this.classRegeneration = classRegeneration;
076        }
077    
078        public InliningContext subInline(NameGenerator generator) {
079            return subInline(generator, Collections.<String, String>emptyMap());
080        }
081    
082        public InliningContext subInlineLambda(LambdaInfo lambdaInfo) {
083            Map<String, String> map = new HashMap();
084            map.put(lambdaInfo.getLambdaClassType().getInternalName(), null); //mark lambda inlined
085            return subInline(nameGenerator.subGenerator("lambda"), map, true);
086        }
087    
088        public InliningContext subInline(NameGenerator generator, Map<String, String> additionalTypeMappings) {
089            return subInline(generator, additionalTypeMappings, isInliningLambda);
090        }
091    
092        public InliningContext subInline(NameGenerator generator, Map<String, String> additionalTypeMappings, boolean isInliningLambda) {
093            Map<String, String> newTypeMappings = new HashMap<String, String>(typeMapping);
094            newTypeMappings.putAll(additionalTypeMappings);
095            return new InliningContext(expressionMap, invokeCalls, constructorInvocation, remapper, state, generator, startContext, call,
096                                    newTypeMappings, isInliningLambda, classRegeneration);
097        }
098    
099        public InliningContext classRegeneration() {
100            return new InliningContext(expressionMap, invokeCalls, constructorInvocation, remapper, state, nameGenerator, startContext, call,
101                                       typeMapping, isInliningLambda, true);
102        }
103    }