view sema/Scope.d @ 168:7982eb63c0eb

Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
author Anders Johnsen <skabet@gmail.com>
date Thu, 24 Jul 2008 12:06:48 +0200
parents 6c5a3c0bb4fb
children
line wrap: on
line source

module sema.Scope;

import tango.io.Stdout;

import lexer.Token,
       ast.Module,
       ast.Decl;

public 
import sema.DType,
       sema.Symbol;

class Scope
{
    this() {}
    this(Scope enclosing)
    {
        this.enclosing = enclosing;
        this.func = enclosing.func;
        this.inModule = enclosing.inModule;
        this.mHandle = enclosing.mHandle;
    }

    Scope enclosing;
    ModuleHandler mHandle;
    Module inModule;
    Scope[] imported;

    ImportDecl[] imports;

    void put(char[] id, Decl d)
    {
        symbols[id] ~= d;
    }

    Decl[] find(char[] id)
    {
        if(id is "")
            return null;
        else if (auto sym = id in symbols)
        {
            return *sym;
        }
        else if (enclosing !is null)
        {
            auto res = enclosing.find(id) ~ mHandle.find(getImports, id);
            return res;
        }
        return [];
    }

    ImportDecl[] getImports()
    {
        if(enclosing)
            return enclosing.getImports ~ imports;
        return imports;
    }

    DType findType(char[] id)
    {
        if (auto type = id in types)
            return *type;
        if (enclosing !is null)
        {
            auto type = enclosing.findType(id);
            if(type is null)
                return mHandle.findType(getImports, id);
            return type;
        }
        return null;
    }

    FuncDecl parentFunction()
    {
        if (func !is null)
            return func;
        else if (enclosing !is null)
            return enclosing.parentFunction();
        else
            return null;
    }

    int stmtIndex()
    {
        if (currentStmtIndex != -1)
            return currentStmtIndex;
        else if (enclosing !is null)
            return enclosing.stmtIndex();
        else
            return -1;
    }

    int opEquals(Object o)
    {
        return this is o;
    }

    char[] toString()
    {
        if (func)
            return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length);
        return Stdout.layout.convert("root: {}", symbols.length);
    }

    FuncDecl parentFunction(FuncDecl f)
    {
        func = f;
        return f;
    }
    DType[char[]] types;
    int currentStmtIndex = -1;
private:
    Decl[][char[]] symbols;
    FuncDecl func;
}

class ModuleHandler
{
    void add(Module m)
    {
        modules[m.moduleName] = m;
    }
    void add(Module m, char[] file)
    {
        fileToModule[file] = m.moduleName;
        add(m);
    }

    DType findType(ImportDecl[] imports, char[] type)
    {
        foreach(i ; imports)
            if(i.get in modules)
            {
                auto t = modules[i.get].env.findType(type);
                if(t !is null)
                    return t;
            }
        return null;
    }

    Decl[] find(ImportDecl[] imports, char[] id)
    {
        foreach(i ; imports)
            if(i.get in modules)
            {
                auto t = modules[i.get].env.find(id);
                if(t !is null)
                    return t;
            }
        return [];
    }

    char[][char[]] fileToModule;
    Module[char[]] modules;
}