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, ResolutionResultsCacheImpl.create(), LabelResolver.create(),
044                              expressionTypingServices.createExtension(scope, false), false);
045        }
046    
047        @NotNull
048        public static ExpressionTypingContext newContext(@NotNull ResolutionContext resolutionContext) {
049            return newContext(resolutionContext.trace, resolutionContext.scope, resolutionContext.dataFlowInfo,
050                              resolutionContext.expectedType, resolutionContext.contextDependency,
051                              resolutionContext.resolutionResultsCache, resolutionContext.labelResolver,
052                              resolutionContext.callResolverExtension, resolutionContext.isAnnotationContext);
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 LabelResolver labelResolver,
064                @NotNull CallResolverExtension callResolverExtension,
065                boolean isAnnotationContext
066        ) {
067            return new ExpressionTypingContext(labelResolver, trace, scope, scope.getContainingDeclaration(), dataFlowInfo,
068                                               expectedType, contextDependency, resolutionResultsCache, callResolverExtension, isAnnotationContext);
069        }
070    
071        public final DeclarationDescriptor containingDeclaration;
072        private CompileTimeConstantChecker compileTimeConstantChecker;
073    
074        private ExpressionTypingContext(
075                @NotNull LabelResolver labelResolver,
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        ) {
086            super(trace, scope, expectedType, dataFlowInfo, contextDependency, resolutionResultsCache, labelResolver, callResolverExtension,
087                  isAnnotationContext);
088            this.containingDeclaration = containingDeclaration;
089        }
090    
091        @Override
092        protected ExpressionTypingContext create(
093                @NotNull BindingTrace trace,
094                @NotNull JetScope scope,
095                @NotNull DataFlowInfo dataFlowInfo,
096                @NotNull JetType expectedType,
097                @NotNull ContextDependency contextDependency,
098                @NotNull ResolutionResultsCache resolutionResultsCache,
099                @NotNull LabelResolver labelResolver
100        ) {
101            return new ExpressionTypingContext(this.labelResolver, trace, scope, containingDeclaration, dataFlowInfo,
102                                               expectedType, contextDependency, resolutionResultsCache, callResolverExtension,
103                                               isAnnotationContext);
104        }
105    
106    ///////////// LAZY ACCESSORS
107    
108        public CompileTimeConstantChecker getCompileTimeConstantChecker() {
109            if (compileTimeConstantChecker == null) {
110                compileTimeConstantChecker = new CompileTimeConstantChecker(trace, false);
111            }
112            return compileTimeConstantChecker;
113        }
114    }