view sema/Declarations.d @ 29:41d23f2762c3 new_gen

Merge, and updated Error class Usage is something like: --- auto e = new Error("No conversion between %0 and %1); e.arg(t1).arg(t2); e.loc(exp.location); --- Multiple locations can be given, to do clang like errors in the future
author Anders Halager <halager@gmail.com>
date Sun, 20 Apr 2008 11:47:34 +0200
parents 69464d465284
children 3147a52d1247
line wrap: on
line source

module sema.Declarations;

import sema.Visitor,
       sema.DType;

import tango.io.Stdout;

import misc.Error;

class Declarations : Visitor!(void)
{
    int[char[]] types;

    private Error error(uint line, char[] msg)
    {
        return new Error(msg);
    }

    override void visitIdentifier(Identifier i)
    {
        auto symbol = i.env.find(i);

        if(symbol is null)
            throw error(__LINE__, "Undefined identifier: '%0'")
                .arg(i.get)
                .loc(i.token.location);
    }

    override void visitVarDecl(VarDecl d)
    {
        if(!d.env.findType(d.type))
            throw error(__LINE__, "Undefined type: '%0'")
                .arg(d.type.get)
                .loc(d.type.token.location);

        visitExp(d.identifier);
        if (d.init)
            visitExp(d.init);
    }
    override void visitFuncDecl(FuncDecl f)
    {
        visitExp(f.identifier);

        foreach (stmt; f.statements)
            visitStmt(stmt);
    }

    override void visitMemberLookup(MemberLookup m)
    {
        switch(m.target.expType)
        {
            case ExpType.Identifier:
                auto target = cast(Identifier)m.target;
                auto child = m.child;
                auto st = cast(DStruct)(target.env.find(target).type);
                if((child.get in st.members) == null)
                    throw new Error("Undefined member in "~target.get~" with type "~st.name,st.getLoc);
        }
    }


    bool isType(char[] s)
    {
        return (s in types? true : false);
    }

}