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
017package org.jetbrains.jet.lang.resolve;
018
019import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
020import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
021import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
022import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
023
024public class OverloadUtil {
025
026    /**
027     * Does not check names.
028     */
029    public static OverloadCompatibilityInfo isOverloadable(CallableDescriptor a, CallableDescriptor b) {
030        int abc = braceCount(a);
031        int bbc = braceCount(b);
032        
033        if (abc != bbc) {
034            return OverloadCompatibilityInfo.success();
035        }
036        
037        OverridingUtil.OverrideCompatibilityInfo overrideCompatibilityInfo = OverridingUtil.isOverridableByImpl(a, b, false);
038        switch (overrideCompatibilityInfo.getResult()) {
039            case OVERRIDABLE:
040            case CONFLICT:
041                return OverloadCompatibilityInfo.someError();
042            case INCOMPATIBLE:
043                return OverloadCompatibilityInfo.success();
044            default:
045                throw new IllegalStateException();
046        }
047    }
048    
049    private static int braceCount(CallableDescriptor a) {
050        if (a instanceof PropertyDescriptor) {
051            return 0;
052        }
053        else if (a instanceof SimpleFunctionDescriptor) {
054            return 1;
055        }
056        else if (a instanceof ConstructorDescriptor) {
057            return 1;
058        }
059        else {
060            throw new IllegalStateException();
061        }
062    }
063
064    public static class OverloadCompatibilityInfo {
065
066        private static final OverloadCompatibilityInfo SUCCESS = new OverloadCompatibilityInfo(true, "SUCCESS");
067        
068        public static OverloadCompatibilityInfo success() {
069            return SUCCESS;
070        }
071        
072        public static OverloadCompatibilityInfo someError() {
073            return new OverloadCompatibilityInfo(false, "XXX");
074        }
075        
076
077        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
078
079        private final boolean isSuccess;
080        private final String message;
081
082        public OverloadCompatibilityInfo(boolean success, String message) {
083            isSuccess = success;
084            this.message = message;
085        }
086
087        public boolean isSuccess() {
088            return isSuccess;
089        }
090
091        public String getMessage() {
092            return message;
093        }
094
095    }
096
097}