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