// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.android.tools.r8.graph;

public interface DexDefinitionSupplier {

  /**
   * Lookup for the definition of a type independent of context.
   *
   * <p>This will make use of the compilers program, library and classpath precedence.
   *
   * @param type Type to look up the defintion for.
   * @return Definition of the type or null if no definition exists.
   */
  default DexClass contextIndependentDefinitionFor(DexType type) {
    return definitionFor(type);
  }

  /**
   * Lookup for the definition of a type from a given context.
   *
   * <p>This ensures that a context overrides the usual lookup precedence if looking up itself.
   *
   * @param type Type to look up a definition for.
   * @param context Context from which the lookup is taking place.
   * @return Definition of the type or null if no definition exists.
   */
  default DexClass definitionFor(DexType type, DexProgramClass context) {
    return type == context.type ? context : contextIndependentDefinitionFor(type);
  }

  default DexClass definitionFor(DexType type, ProgramMethod context) {
    return definitionFor(type, context.getHolder());
  }

  /**
   * Lookup for the program definition of a type from a given context.
   *
   * <p>This ensures that a context overrides the usual lookup precedence if looking up itself.
   *
   * @param type Type to look up a definition for.
   * @param context Context from which the lookup is taking place.
   * @return Definition of the type if it is a program type or null if not or no definition exists.
   */
  default DexProgramClass programDefinitionFor(DexType type, DexProgramClass context) {
    return DexProgramClass.asProgramClassOrNull(definitionFor(type, context));
  }

  default DexProgramClass programDefinitionFor(DexType type, ProgramMethod context) {
    return programDefinitionFor(type, context.getHolder());
  }

  default <D extends DexEncodedMember<D, R>, R extends DexMember<D, R>>
      DexClass definitionForHolder(DexEncodedMember<D, R> member, ProgramMethod context) {
    return definitionForHolder(member.toReference(), context.getHolder());
  }

  default <D extends DexEncodedMember<D, R>, R extends DexMember<D, R>>
      DexClass definitionForHolder(DexEncodedMember<D, R> member, DexProgramClass context) {
    return definitionForHolder(member.toReference(), context);
  }

  default <D extends DexEncodedMember<D, R>, R extends DexMember<D, R>>
      DexClass definitionForHolder(DexMember<D, R> member, ProgramMethod context) {
    return definitionFor(member.holder, context.getHolder());
  }

  default <D extends DexEncodedMember<D, R>, R extends DexMember<D, R>>
      DexClass definitionForHolder(DexMember<D, R> member, DexProgramClass context) {
    return definitionFor(member.holder, context);
  }

  // Use definitionFor with a context or contextIndependentDefinitionFor without.
  @Deprecated
  DexClass definitionFor(DexType type);

  // Use programDefinitionFor with a context.
  @Deprecated
  default DexProgramClass definitionForProgramType(DexType type) {
    return DexProgramClass.asProgramClassOrNull(definitionFor(type));
  }

  // Use definitionForHolder with a context.
  @Deprecated
  default <D extends DexEncodedMember<D, R>, R extends DexMember<D, R>>
      DexClass definitionForHolder(DexMember<D, R> member) {
    return definitionFor(member.holder);
  }

  DexItemFactory dexItemFactory();
}
