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.FunctionDescriptor;
021    import org.jetbrains.jet.lang.psi.Call;
022    import org.jetbrains.jet.lang.psi.JetReferenceExpression;
023    import org.jetbrains.jet.lang.resolve.BindingTrace;
024    import org.jetbrains.jet.lang.resolve.calls.CallResolverExtension;
025    import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
026    import org.jetbrains.jet.lang.resolve.calls.context.*;
027    import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResults;
028    import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstantResolver;
029    import org.jetbrains.jet.lang.resolve.name.Name;
030    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
031    import org.jetbrains.jet.lang.types.JetType;
032    
033    public class ExpressionTypingContext extends ResolutionContext<ExpressionTypingContext> {
034    
035        @NotNull
036        public static ExpressionTypingContext newContext(
037                @NotNull ExpressionTypingServices expressionTypingServices,
038                @NotNull BindingTrace trace,
039                @NotNull JetScope scope,
040                @NotNull DataFlowInfo dataFlowInfo,
041                @NotNull JetType expectedType
042        ) {
043            return newContext(expressionTypingServices, trace, scope, dataFlowInfo, expectedType,
044                              ContextDependency.INDEPENDENT, ResolutionResultsCacheImpl.create(), LabelResolver.create(),
045                              expressionTypingServices.createExtension(scope, false), false);
046        }
047    
048        @NotNull
049        public static ExpressionTypingContext newContext(
050                @NotNull ExpressionTypingServices expressionTypingServices,
051                @NotNull ResolutionContext resolutionContext
052        ) {
053            return newContext(expressionTypingServices, resolutionContext.trace, resolutionContext.scope, resolutionContext.dataFlowInfo,
054                              resolutionContext.expectedType, resolutionContext.contextDependency,
055                              resolutionContext.resolutionResultsCache, resolutionContext.labelResolver,
056                              resolutionContext.callResolverExtension, resolutionContext.isAnnotationContext);
057        }
058    
059        @NotNull
060        public static ExpressionTypingContext newContext(
061                @NotNull ExpressionTypingServices expressionTypingServices,
062                @NotNull BindingTrace trace,
063                @NotNull JetScope scope,
064                @NotNull DataFlowInfo dataFlowInfo,
065                @NotNull JetType expectedType,
066                @NotNull ContextDependency contextDependency,
067                @NotNull ResolutionResultsCache resolutionResultsCache,
068                @NotNull LabelResolver labelResolver,
069                @NotNull CallResolverExtension callResolverExtension,
070                boolean isAnnotationContext
071        ) {
072            return new ExpressionTypingContext(expressionTypingServices, labelResolver, trace, scope, dataFlowInfo, expectedType,
073                                               contextDependency, resolutionResultsCache, callResolverExtension, isAnnotationContext);
074        }
075    
076        public final ExpressionTypingServices expressionTypingServices;
077    
078        private CompileTimeConstantResolver compileTimeConstantResolver;
079    
080        private ExpressionTypingContext(
081                @NotNull ExpressionTypingServices expressionTypingServices,
082                @NotNull LabelResolver labelResolver,
083                @NotNull BindingTrace trace,
084                @NotNull JetScope scope,
085                @NotNull DataFlowInfo dataFlowInfo,
086                @NotNull JetType expectedType,
087                @NotNull ContextDependency contextDependency,
088                @NotNull ResolutionResultsCache resolutionResultsCache,
089                @NotNull CallResolverExtension callResolverExtension,
090                boolean isAnnotationContext
091        ) {
092            super(trace, scope, expectedType, dataFlowInfo, contextDependency, resolutionResultsCache, labelResolver, callResolverExtension,
093                  isAnnotationContext);
094            this.expressionTypingServices = expressionTypingServices;
095        }
096    
097        @Override
098        protected ExpressionTypingContext create(
099                @NotNull BindingTrace trace,
100                @NotNull JetScope scope,
101                @NotNull DataFlowInfo dataFlowInfo,
102                @NotNull JetType expectedType,
103                @NotNull ContextDependency contextDependency,
104                @NotNull ResolutionResultsCache resolutionResultsCache,
105                @NotNull LabelResolver labelResolver
106        ) {
107            return new ExpressionTypingContext(expressionTypingServices, this.labelResolver, trace, scope, dataFlowInfo, expectedType,
108                                               contextDependency, resolutionResultsCache, callResolverExtension, isAnnotationContext);
109        }
110    
111        @Override
112        protected ExpressionTypingContext self() {
113            return this;
114        }
115    
116    ///////////// LAZY ACCESSORS
117    
118        public CompileTimeConstantResolver getCompileTimeConstantResolver() {
119            if (compileTimeConstantResolver == null) {
120                compileTimeConstantResolver = new CompileTimeConstantResolver();
121            }
122            return compileTimeConstantResolver;
123        }
124    
125    ////////// Call resolution utilities
126    
127        @NotNull
128        public OverloadResolutionResults<FunctionDescriptor> resolveCallWithGivenName(@NotNull Call call, @NotNull JetReferenceExpression functionReference, @NotNull Name name) {
129            return expressionTypingServices.getCallResolver().resolveCallWithGivenName(
130                    BasicCallResolutionContext.create(this, call, CheckValueArgumentsMode.ENABLED),
131                    functionReference,
132                    name
133            );
134        }
135    }