/**
 * Copyright (c) 2013 Stefan Marr, stefan.marr@vub.ac.be
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package som.interpreter;

import java.math.BigInteger;

import som.vm.constants.Blocks;
import som.vm.constants.Classes;
import som.vm.constants.Globals;
import som.vmobjects.SAbstractObject;
import som.vmobjects.SBlock;
import som.vmobjects.SBlock.SBlock1;
import som.vmobjects.SBlock.SBlock2;
import som.vmobjects.SBlock.SBlock3;
import som.vmobjects.SClass;
import som.vmobjects.SInvokable;
import som.vmobjects.SInvokable.SMethod;
import som.vmobjects.SInvokable.SPrimitive;
import som.vmobjects.SObject;
import som.vmobjects.SSymbol;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.dsl.TypeSystem;

@TypeSystem({   boolean.class,
                   long.class,
             BigInteger.class,
                 String.class,
                 double.class,
                 SClass.class,
                SObject.class,
                 SBlock.class,
                SSymbol.class,
             SInvokable.class,
        SAbstractObject.class,
               Object[].class})
public class Types {

  public static SClass getClassOf(final Object obj) {
    CompilerAsserts.neverPartOfCompilation();
    assert obj != null;

    if (obj instanceof SAbstractObject) {
      return ((SAbstractObject) obj).getSOMClass();
    } else if (obj instanceof Boolean) {
      if ((boolean) obj) {
        return Globals.trueClass;
      } else {
        return Globals.falseClass;
      }
    } else if (obj instanceof Long || obj instanceof BigInteger) {
      return Classes.integerClass;
    } else if (obj instanceof String) {
      return Classes.stringClass;
    } else if (obj instanceof Double) {
      return Classes.doubleClass;
    } else if (obj instanceof SMethod) {
      return Classes.methodClass;
    } else if (obj instanceof SPrimitive) {
      return Classes.primitiveClass;
    } else if (obj instanceof SBlock1) {
      return Blocks.blockClass1;
    } else if (obj instanceof SBlock2) {
      return Blocks.blockClass2;
    } else if (obj instanceof SBlock3) {
      return Blocks.blockClass3;
    } else if (obj instanceof Object[]) {
      return Classes.arrayClass;
    }

    TruffleCompiler.transferToInterpreter("Should not be reachable");
    throw new RuntimeException("We got an object that should be covered by the above check: " + obj.toString());
  }
}
