view sema/Declarations.d @ 31:d14705a06bdd new_gen

Declarations.d/visitMemberLookup: Use the loc of the lookup, rather than the types loc
author Anders Halager <halager@gmail.com>
date Sun, 20 Apr 2008 12:03:13 +0200
parents 3147a52d1247
children 1a7a308f75b2
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 error(__LINE__, "Undefined member in %0 with type %1")
                        .arg(target.get)
                        .arg(st.name)
                        .loc(target.token.location)
                        .loc(child.token.location);
        }
    }


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

}