view sema/DType.d @ 37:858b9805843d new_gen

Bug-fixes Void can now be used and is recognized as a keyword by lexer Fixed a problem with casting on pointer types The expression is now optional for a ReturnStmt (only legal in void funcs)
author Anders Halager <halager@gmail.com>
date Sun, 20 Apr 2008 23:53:05 +0200
parents 69464d465284
children 1a7a308f75b2
line wrap: on
line source

module sema.DType;

import tango.text.Util : jhash;

import LLVM = llvm.llvm;

import lexer.Token,
       ast.Exp;

import tango.io.Stdout;

class DType
{
    private char[] id;
    private Location loc;
    public DType actual;
    private LLVM.Type llvmType;

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

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

    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;
    }

    hash_t toHash()
    {
        return cast(hash_t)(cast(void*)this);
    }

    char[] name() { return id; }
    Location getLoc() { return loc; }
    LLVM.Type llvm() { return llvmType; }

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

    static DType Void;

    static this()
    {
        Void   = new DType("void");
        Void.llvmType = LLVM.Type.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 DInteger : DType
{
    this(char[] name, int bits, bool unsigned)
    {
        super(name, null);
        this.bits = bits;
        this.unsigned = unsigned;
        llvmType = LLVM.IntegerType.Get(bits);
    }

    int bits;
    bool unsigned;
}

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

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

        LLVM.Type[] types;

        foreach( type ; members)
            types ~= type.llvm;

        this.llvmType = LLVM.StructType.Get(types);

    }
    DType[char[]] members;
}

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