/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.lookup;
//import checkers.inference.ownership.quals.*;

import java.util.Map;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

/**
 * Binding for a type parameter, held by source/binary type or method.
 */
public class TypeVariableBinding extends ReferenceBinding {

	public Binding declaringElement; // binding of declaring type or method 
	public int rank; // declaration rank, can be used to match variable in parameterized type

	/**
	 * Denote the first explicit (binding) bound amongst the supertypes (from declaration in source)
	 * If no superclass was specified, then it denotes the first superinterface, or null if none was specified.
	 */
	public TypeBinding firstBound; 

	// actual resolved variable supertypes (if no superclass bound, then associated to Object)
	public ReferenceBinding superclass;
	public ReferenceBinding[] superInterfaces; 
	public char[] genericTypeSignature;

	public TypeVariableBinding(char[] sourceName, Binding declaringElement, int rank) {
		this.sourceName = sourceName;
		this.declaringElement = declaringElement;
		this.rank = rank;
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
		this.tagBits |= HasTypeVariable;
	}

	public int kind() {
		return TYPE_PARAMETER;
	}	
	
	/**
	 * Returns true if the argument type satisfies all bounds of the type parameter
	 */
	public int boundCheck(Substitution substitution, TypeBinding argumentType) {

		if (argumentType == NullBinding || argumentType == this)
			return TypeConstants.OK;
		boolean hasSubstitution = substitution != null;
		if (!(argumentType instanceof ReferenceBinding || argumentType.isArrayType()))
			return TypeConstants.MISMATCH;	
		// special case for re-entrant source types (selection, code assist, etc)...
		// can request additional types during hierarchy walk that are found as source types that also 'need' to connect their hierarchy
		if (this.superclass == null)
			return TypeConstants.OK;

		if (argumentType.isWildcard() && !argumentType.isIntersectionType()) {
			WildcardBinding wildcard = (WildcardBinding) argumentType;
			switch(wildcard.boundKind) {
				case Wildcard.EXTENDS :
					TypeBinding wildcardBound = wildcard.bound;
					if (wildcardBound == this) 
						return TypeConstants.OK;
					ReferenceBinding superclassBound = hasSubstitution ? (ReferenceBinding)Scope.substitute(substitution, this.superclass) : this.superclass;
					boolean isArrayBound = wildcardBound.isArrayType();
					if (!wildcardBound.isInterface()) {
						if (superclassBound.id != T_JavaLangObject) {
							if (isArrayBound) {
								if (!wildcardBound.isCompatibleWith(superclassBound))
									return TypeConstants.MISMATCH;
							} else {
								ReferenceBinding match = ((ReferenceBinding)wildcardBound).findSuperTypeWithSameErasure(superclassBound);
								if (match != null) {
									if (!match.isIntersectingWith(superclassBound)) {
										return TypeConstants.MISMATCH;
									}
								} else {
									return TypeConstants.MISMATCH;
								}
							}
						}
					}
					ReferenceBinding[] superInterfaceBounds = hasSubstitution ? Scope.substitute(substitution, this.superInterfaces) : this.superInterfaces;
					int length = superInterfaceBounds.length;
					boolean mustImplement = isArrayBound || ((ReferenceBinding)wildcardBound).isFinal();
					for (int i = 0; i < length; i++) {
						TypeBinding superInterfaceBound = superInterfaceBounds[i];
						if (isArrayBound) {
							if (!wildcardBound.isCompatibleWith(superInterfaceBound))
									return TypeConstants.MISMATCH;
						} else {
							ReferenceBinding match = ((ReferenceBinding)wildcardBound).findSuperTypeWithSameErasure(superInterfaceBound);
							if (match != null) {
								if (!match.isIntersectingWith(superInterfaceBound)) {
									return TypeConstants.MISMATCH;
								}
							} else if (mustImplement) {
									return TypeConstants.MISMATCH; // cannot be extended further to satisfy missing bounds
							}
						}

					}
					break;
					
				case Wildcard.SUPER :
					return boundCheck(substitution, wildcard.bound);
					
				case Wildcard.UNBOUND :
					break;
			}
			return TypeConstants.OK;
		}
		boolean unchecked = false;
		if (this.superclass.id != T_JavaLangObject) {
			TypeBinding superType = this.superclass;
			if (superType != argumentType) { // check identity before substituting (104649)
				TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, superType) : superType;
				if (!argumentType.isCompatibleWith(substitutedSuperType)) {
				    return TypeConstants.MISMATCH;
				}
				if (argumentType instanceof ReferenceBinding) {
					ReferenceBinding referenceArgument = (ReferenceBinding) argumentType;
					TypeBinding match = referenceArgument.findSuperTypeWithSameErasure(substitutedSuperType);
					if (match != null){
						// Enum#RAW is not a substitute for <E extends Enum<E>> (86838)
						if (match.isRawType() && substitutedSuperType.isBoundParameterizedType())
							unchecked = true;
					}
				} 
			}
		}
	    for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
	    	TypeBinding superType = this.superInterfaces[i];
	    	if (superType != argumentType) { // check identity before substituting (104649)
				TypeBinding substitutedSuperType = hasSubstitution ? Scope.substitute(substitution, superType) : superType;
				if (!argumentType.isCompatibleWith(substitutedSuperType)) {
				    return TypeConstants.MISMATCH;
				}
				if (argumentType instanceof ReferenceBinding) {
					ReferenceBinding referenceArgument = (ReferenceBinding) argumentType;
					TypeBinding match = referenceArgument.findSuperTypeWithSameErasure(substitutedSuperType);
					if (match != null){
						// Enum#RAW is not a substitute for <E extends Enum<E>> (86838)
						if (match.isRawType() && substitutedSuperType.isBoundParameterizedType())
							unchecked = true;
					}
				}
	    	}
	    }
	    return unchecked ? TypeConstants.UNCHECKED : TypeConstants.OK;
	}
	
	/**
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#canBeInstantiated()
	 */
	public boolean canBeInstantiated() {
		return false;
	}
	/**
	 * Collect the substitutes into a map for certain type variables inside the receiver type
	 * e.g.   Collection<T>.collectSubstitutes(Collection<List<X>>, Map), will populate Map with: T --> List<X>
	 * Constraints:
	 *   A << F   corresponds to:   F.collectSubstitutes(..., A, ..., 1)
	 *   A = F   corresponds to:      F.collectSubstitutes(..., A, ..., 0)
	 *   A >> F   corresponds to:   F.collectSubstitutes(..., A, ..., 2)
	 */
	public void collectSubstitutes(Scope scope, TypeBinding actualType, Map substitutes, int constraint) {
		
		// cannot infer anything from a null type
		switch (actualType.kind()) {
			case Binding.BASE_TYPE :
				if (actualType == NullBinding) return;
				TypeBinding boxedType = scope.environment().computeBoxingType(actualType);
				if (boxedType == actualType) return;
				actualType = boxedType;
				break;
			case Binding.WILDCARD_TYPE :
				WildcardBinding actualWildcard = (WildcardBinding) actualType;
				if (actualWildcard.otherBounds != null) break; // intersection type
				return; // wildcards are not true type expressions (JLS 15.12.2.7, p.453 2nd discussion)
		}
	
		// reverse constraint, to reflect variable on rhs:   A << T --> T >: A
		int variableConstraint;
		switch(constraint) {
			case CONSTRAINT_EQUAL :
				variableConstraint = CONSTRAINT_EQUAL;
				break;
			case CONSTRAINT_EXTENDS :
				variableConstraint = CONSTRAINT_SUPER;
				break;
			default:
			//case CONSTRAINT_SUPER :
				variableConstraint = CONSTRAINT_EXTENDS;
				break;
		}
	    TypeBinding[][] variableSubstitutes = (TypeBinding[][])substitutes.get((/*@OwnPar*/ /*@NoRep*/ TypeVariableBinding)this);
	    if (variableSubstitutes != null) {
		    insertLoop: {
		    	TypeBinding[] constraintSubstitutes = variableSubstitutes[variableConstraint];
		    	int length;
		    	if (constraintSubstitutes == null) {
		    		length = 0;
		    		constraintSubstitutes = new TypeBinding[1];
		    	} else {
		    		length = constraintSubstitutes.length;
			        for (int i = 0; i < length; i++) {
			        	TypeBinding substitute = constraintSubstitutes[i];
			            if (substitute == actualType) return; // already there
			            if (substitute == null) {
			                constraintSubstitutes[i] = actualType;
			                break insertLoop;
			            }
			        }
			        // no free spot found, need to grow by one
			        System.arraycopy(constraintSubstitutes, 0, constraintSubstitutes = new TypeBinding[length+1], 0, length);
		    	}
		        constraintSubstitutes[length] = actualType;
		        variableSubstitutes[variableConstraint] = constraintSubstitutes;
		    }
	    }
	}
	
	public char[] constantPoolName() { /* java/lang/Object */ 
	    if (this.firstBound != null) {
			return this.firstBound.constantPoolName();
	    }
	    return this.superclass.constantPoolName(); // java/lang/Object
	}
	/*
	 * declaringUniqueKey : genericTypeSignature
	 * p.X<T> { ... } --> Lp/X;:TT;
	 * p.X { <T> void foo() {...} } --> Lp/X;.foo()V:TT;
	 */
	public char[] computeUniqueKey(boolean isLeaf) {
		/*@RepRep*/ StringBuffer buffer = new StringBuffer();
		Binding declaring = this.declaringElement;
		if (!isLeaf && declaring.kind() == Binding.METHOD) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=97902
			MethodBinding methodBinding = (MethodBinding) declaring;
			ReferenceBinding declaringClass = methodBinding.declaringClass;
			buffer.append(declaringClass.computeUniqueKey(false/*not a leaf*/));
			buffer.append(':');
			MethodBinding[] methods = declaringClass.methods();
			if (methods != null)
				for (int i = 0, length = methods.length; i < length; i++) {
					MethodBinding binding = methods[i];
					if (binding == methodBinding) {
						buffer.append(i);
						break;
					}
				}
		} else {
			buffer.append(declaring.computeUniqueKey(false/*not a leaf*/));
			buffer.append(':');			
		}
		buffer.append(genericTypeSignature());
		int length = buffer.length();
		char[] uniqueKey = new char[length];
		buffer.getChars(0, length, uniqueKey, 0);
		return uniqueKey;
	}
	/**
	 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#debugName()
	 */
	public String debugName() {
	    return new String(this.sourceName);		
	}		
	public TypeBinding erasure() {
	    if (this.firstBound != null) {
			return this.firstBound.erasure();
	    }
	    return this.superclass; // java/lang/Object
	}	

	/**
	 * T::Ljava/util/Map;:Ljava/io/Serializable;
	 * T:LY<TT;>
	 */
	public char[] genericSignature() {
	    /*@RepRep*/ StringBuffer sig = new StringBuffer(10);
	    sig.append(this.sourceName).append(':');
	   	int interfaceLength = this.superInterfaces == null ? 0 : this.superInterfaces.length;
	    if (interfaceLength == 0 || this.firstBound == this.superclass) {
	    	if (this.superclass != null)
		        sig.append(this.superclass.genericTypeSignature());
	    }
		for (int i = 0; i < interfaceLength; i++) {
		    sig.append(':').append(this.superInterfaces[i].genericTypeSignature());
		}
		int sigLength = sig.length();
		char[] genericSignature = new char[sigLength];
		sig.getChars(0, sigLength, genericSignature, 0);					
		return genericSignature;
	}
	/**
	 * T::Ljava/util/Map;:Ljava/io/Serializable;
	 * T:LY<TT;>
	 */
	public char[] genericTypeSignature() {
	    if (this.genericTypeSignature != null) return this.genericTypeSignature;
		return this.genericTypeSignature = CharOperation.concat('T', this.sourceName, ';');
	}

	/**
	 * Returns true if the type variable is directly bound to a given type
	 */
	public boolean isErasureBoundTo(TypeBinding type) {
		if (this.superclass.erasure() == type) 
			return true;
		for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
			if (this.superInterfaces[i].erasure() == type)
				return true;
		}
		return false;
	}
	
	/**
	 * Returns true if the 2 variables are playing exact same role: they have
	 * the same bounds, providing one is substituted with the other: <T1 extends
	 * List<T1>> is interchangeable with <T2 extends List<T2>>.
	 */
	public boolean isInterchangeableWith(final LookupEnvironment environment, final TypeVariableBinding otherVariable) {
		if (this == otherVariable)
			return true;
		int length = this.superInterfaces.length;
		if (length != otherVariable.superInterfaces.length)
			return false;

		identical: {
			if (this.superclass != otherVariable.superclass) {
				if (this.superclass.erasure() != otherVariable.superclass.erasure())
					return false; // no way it can match after substitution
				break identical;
			}
			for (int i = 0; i < length; i++) {
				if (this.superInterfaces[i] != otherVariable.superInterfaces[i]) {
					if (this.superInterfaces[i].erasure() != otherVariable.superInterfaces[i].erasure())
						return false; // no way it can match after substitution
					break identical;
				}
			}
			return true;
		}
		// need substitutions
		Substitution subst = new Substitution() {
			public LookupEnvironment environment() { return environment; }
			public boolean isRawSubstitution() { return false; }
			public TypeBinding substitute(TypeVariableBinding typeVariable) {
				return typeVariable == otherVariable ? (/*@OwnPar*/ /*@NoRep*/ TypeVariableBinding) TypeVariableBinding.this : typeVariable;
			}
		};
		if (this.superclass != Scope.substitute(subst, otherVariable.superclass))
			return false;
		for (int i = 0; i < length; i++)
			if (this.superInterfaces[i] != Scope.substitute(subst, otherVariable.superInterfaces[i]))
				return false;
		return true;
	}
	
	/**
	 * Returns true if the type was declared as a type variable
	 */
	public boolean isTypeVariable() {
	    return true;
	}

	/** 
	 * Returns the original type variable for a given variable.
	 * Only different from receiver for type variables of generic methods of parameterized types
	 * e.g. X<U> {   <V1 extends U> U foo(V1)   } --> X<String> { <V2 extends String> String foo(V2)  }  
	 *         and V2.original() --> V1
	 */
	public TypeVariableBinding original() {
		if (this.declaringElement.kind() == Binding.METHOD) {
			MethodBinding originalMethod = ((MethodBinding)this.declaringElement).original();
			if (originalMethod != this.declaringElement) {
				return originalMethod.typeVariables[this.rank];
			}
		} else {
			ReferenceBinding originalType = (ReferenceBinding)((ReferenceBinding)this.declaringElement).erasure();
			if (originalType != this.declaringElement) {
				return originalType.typeVariables()[this.rank];
			}
		}
		return (/*@OwnPar*/ /*@NoRep*/ TypeVariableBinding)this;
	}
	
	/**
     * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#readableName()
     */
    public char[] readableName() {
        return this.sourceName;
    }
   
	ReferenceBinding resolve(LookupEnvironment environment) {
		if ((this.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0)
			return (/*@OwnPar*/ /*@NoRep*/ TypeVariableBinding)this;

		TypeBinding oldSuperclass = this.superclass, oldFirstInterface = null;
		if (this.superclass != null)
			this.superclass = BinaryTypeBinding.resolveUnresolvedType(this.superclass, environment, true);
		ReferenceBinding[] interfaces = this.superInterfaces;
		int length;
		if ((length = interfaces.length) != 0) {
			oldFirstInterface = interfaces[0];
			for (int i = length; --i >= 0;) {
				interfaces[i] = BinaryTypeBinding.resolveUnresolvedType(interfaces[i], environment, true);
			}
		}
		this.modifiers &= ~ExtraCompilerModifiers.AccUnresolved;
	
		// finish resolving the types
		if (this.superclass != null)
			this.superclass = BinaryTypeBinding.resolveType(this.superclass, environment, true);
		for (int i = interfaces.length; --i >= 0;)
			interfaces[i] = BinaryTypeBinding.resolveType(interfaces[i], environment, true);

		// refresh the firstBound in case it changed
		if (this.firstBound != null) {
			if (this.firstBound == oldSuperclass) {
				this.firstBound = this.superclass;
			} else if (this.firstBound == oldFirstInterface) {
				this.firstBound = interfaces[0];
			}
		}
		return (/*@OwnPar*/ /*@NoRep*/ TypeVariableBinding)this;
	}
	
	/**
     * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#shortReadableName()
     */
    public char[] shortReadableName() {
        return this.readableName();
    }
	public ReferenceBinding superclass() {
		return superclass;
	}
	public ReferenceBinding[] superInterfaces() {
		return superInterfaces;
	}	
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer(10);
		buffer.append('<').append(this.sourceName);//.append('[').append(this.rank).append(']');
		if (this.superclass != null && this.firstBound == this.superclass) {
		    buffer.append(" extends ").append(this.superclass.debugName()); //$NON-NLS-1$
		}
		if (this.superInterfaces != null && this.superInterfaces != NoSuperInterfaces) {
		   if (this.firstBound != this.superclass) {
		        buffer.append(" extends "); //$NON-NLS-1$
	        }
		    for (int i = 0, length = this.superInterfaces.length; i < length; i++) {
		        if (i > 0 || this.firstBound == this.superclass) {
		            buffer.append(" & "); //$NON-NLS-1$
		        }
				buffer.append(this.superInterfaces[i].debugName());
			}
		}
		buffer.append('>');
		return buffer.toString();
	}	
	/**
	 * Upper bound doesn't perform erasure
	 */
	public TypeBinding upperBound() {
	    if (this.firstBound != null) {
			return this.firstBound;
	    }
	    return this.superclass; // java/lang/Object
	}		
}
