view sema/DType.d @ 49:c7cde6af0095 new_gen

Seperated the AST from LLVM * Changed SmallArray sligthly * Added a NullAction that doesn't do anything
author Anders Halager <halager@gmail.com>
date Sat, 26 Apr 2008 13:15:37 +0200
parents 1a7a308f75b2
children 79cb0afafabe
line wrap: on
line source

module sema.DType;

import lexer.Token,
       ast.Exp;

import tango.io.Stdout;

class DType
{
    private char[] id;
    private Location loc;
    public DType actual;

    this(Identifier id, DType actual = null)
    {
        Token temp = id.token;
        this.id = temp.get;
        this.loc = temp.location;
        this.actual = actual is null? this : actual;
    }

    this(char[] id, DType actual = null)
    {
        this.id = id;
        this.actual = actual is null? this : actual;
    }

    int opEquals(Object o)
    {
        if (auto t = cast(DType)o)
            return this.actual is t.actual;
        return 0;
    }

    int opCmp(Object o)
    {
        if (auto t = cast(DType)o)
            return cast(void*)this.actual - cast(void*)t.actual;
        return 0;
    }

    /**
      Hashing is done by casting the reference to a void* and taking that
      value, but this gives a bad distribution of hash-values.

      Multiple DType's allocated close to each other will only have a
      difference in the lower bits of their hashes.
     */
    hash_t toHash()
    {
        return cast(hash_t)(cast(void*)this);
    }

    char[] name() { return id; }
    Location getLoc() { return loc; }
    int byteSize() { return 0; }

    static DInteger
        Bool,
        Byte, UByte, Short, UShort,
        Int, UInt, Long, ULong;

    static DType Void;

    static this()
    {
        Void   = new DType("void");

        Bool   = new DInteger("bool",    1, false);
        Byte   = new DInteger("byte",    8, false);
        UByte  = new DInteger("ubyte",   8, true);
        Short  = new DInteger("short",  16, false);
        UShort = new DInteger("ushort", 16, true);
        Int    = new DInteger("int",    32, false);
        UInt   = new DInteger("uint",   32, true);
        Long   = new DInteger("long",   64, false);
        ULong  = new DInteger("ulong",  64, true);
    }
}

/**
  Class to represent the built-in integer types, from byte to long.
 */
class DInteger : DType
{
    this(char[] name, int bits, bool unsigned)
    {
        super(name, null);
        this.bits = bits;
        this.unsigned = unsigned;
    }

    override int byteSize() { return bits / 8; }

    int bits;
    bool unsigned;
}

class DStruct : DType
{
    this(Identifier id, DType actual = null)
    {
        super(id, actual);
    }

    int byteSize() { return bytes_total; }

    void setMembers(DType[char[]] members)
    {
        this.members = members;

        foreach (type; members)
            bytes_total += type.byteSize();
    }

    DType[char[]] members;
    private int bytes_total;
}

class DFunction : DType
{
    this(Identifier id, DType actual = null)
    {
        super(id, actual);
    }

    DType[] params;
    DType return_type;
}