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.lang.types.expressions;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.resolve.BindingTrace;
021    import org.jetbrains.jet.lang.resolve.calls.CallResolverExtension;
022    import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
023    import org.jetbrains.jet.lang.resolve.calls.context.ContextDependency;
024    import org.jetbrains.jet.lang.resolve.calls.context.ResolutionContext;
025    import org.jetbrains.jet.lang.resolve.calls.context.ResolutionResultsCache;
026    import org.jetbrains.jet.lang.resolve.calls.context.ResolutionResultsCacheImpl;
027    import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstantChecker;
028    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
029    import org.jetbrains.jet.lang.types.JetType;
030    
031    public class ExpressionTypingContext extends ResolutionContext<ExpressionTypingContext> {
032    
033        @NotNull
034        public static ExpressionTypingContext newContext(
035                @NotNull ExpressionTypingServices expressionTypingServices,
036                @NotNull BindingTrace trace,
037                @NotNull JetScope scope,
038                @NotNull DataFlowInfo dataFlowInfo,
039                @NotNull JetType expectedType
040        ) {
041            return newContext(trace, scope, dataFlowInfo, expectedType,
042                              ContextDependency.INDEPENDENT, new ResolutionResultsCacheImpl(),
043                              expressionTypingServices.createExtension(scope, false), false);
044        }
045    
046        @NotNull
047        public static ExpressionTypingContext newContext(@NotNull ResolutionContext context) {
048            return new ExpressionTypingContext(
049                    context.trace, context.scope, context.dataFlowInfo, context.expectedType,
050                    context.contextDependency, context.resolutionResultsCache, context.callResolverExtension, context.isAnnotationContext,
051                    context.collectAllCandidates
052            );
053        }
054    
055        @NotNull
056        public static ExpressionTypingContext newContext(
057                @NotNull BindingTrace trace,
058                @NotNull JetScope scope,
059                @NotNull DataFlowInfo dataFlowInfo,
060                @NotNull JetType expectedType,
061                @NotNull ContextDependency contextDependency,
062                @NotNull ResolutionResultsCache resolutionResultsCache,
063                @NotNull CallResolverExtension callResolverExtension,
064                boolean isAnnotationContext
065        ) {
066            return new ExpressionTypingContext(
067                    trace, scope, dataFlowInfo, expectedType, contextDependency,
068                    resolutionResultsCache, callResolverExtension, isAnnotationContext, false);
069        }
070    
071        private CompileTimeConstantChecker compileTimeConstantChecker;
072    
073        private ExpressionTypingContext(
074                @NotNull BindingTrace trace,
075                @NotNull JetScope scope,
076                @NotNull DataFlowInfo dataFlowInfo,
077                @NotNull JetType expectedType,
078                @NotNull ContextDependency contextDependency,
079                @NotNull ResolutionResultsCache resolutionResultsCache,
080                @NotNull CallResolverExtension callResolverExtension,
081                boolean isAnnotationContext,
082                boolean collectAllCandidates
083        ) {
084            super(trace, scope, expectedType, dataFlowInfo, contextDependency, resolutionResultsCache, callResolverExtension,
085                  isAnnotationContext, collectAllCandidates);
086        }
087    
088        @Override
089        protected ExpressionTypingContext create(
090                @NotNull BindingTrace trace,
091                @NotNull JetScope scope,
092                @NotNull DataFlowInfo dataFlowInfo,
093                @NotNull JetType expectedType,
094                @NotNull ContextDependency contextDependency,
095                @NotNull ResolutionResultsCache resolutionResultsCache,
096                boolean collectAllCandidates
097        ) {
098            return new ExpressionTypingContext(trace, scope, dataFlowInfo,
099                                               expectedType, contextDependency, resolutionResultsCache, callResolverExtension,
100                                               isAnnotationContext, collectAllCandidates);
101        }
102    
103    ///////////// LAZY ACCESSORS
104    
105        public CompileTimeConstantChecker getCompileTimeConstantChecker() {
106            if (compileTimeConstantChecker == null) {
107                compileTimeConstantChecker = new CompileTimeConstantChecker(trace, false);
108            }
109            return compileTimeConstantChecker;
110        }
111    }