From 59117fc8bcd610697a45c0013fc7450de2ee512f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 15 Mar 2009 02:27:15 +0000 Subject: Make TEnv strictly keyed by ASymbol. Prettier casting. git-svn-id: http://svn.drobilla.net/resp/tuplr@93 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- llvm.cpp | 23 ++++++----------------- tuplr.hpp | 7 ++++++- typing.cpp | 4 ++-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/llvm.cpp b/llvm.cpp index f9e045a..240cb2f 100644 --- a/llvm.cpp +++ b/llvm.cpp @@ -146,17 +146,6 @@ LITERAL(int32_t, "Int", ConstantInt::get(Type::Int32Ty, val, true)) LITERAL(float, "Float", ConstantFP::get(Type::FloatTy, val)) LITERAL(bool, "Bool", ConstantInt::get(Type::Int1Ty, val, false)) -template -T -checked_cast(AST* ast) -{ - T t = dynamic_cast(ast); - if (!t) - throw Error((format("internal error: `%1%' should be a `%2%'") - % typeid(ast).name() % typeid(T).name()).str(), ast->loc); - return t; -} - static Function* compileFunction(CEnv& cenv, const std::string& name, const Type* retT, const ATuple& protT, const vector argNames=vector()) @@ -165,7 +154,7 @@ compileFunction(CEnv& cenv, const std::string& name, const Type* retT, const ATu vector cprot; for (size_t i = 0; i < protT.size(); ++i) { - AType* at = checked_cast(protT.at(i)); + AType* at = protT.at(i)->as(); if (!lltype(at)) throw Error("function parameter is untyped"); cprot.push_back(lltype(at)); } @@ -226,7 +215,7 @@ AClosure::lift(CEnv& cenv) const_iterator p = prot()->begin(); size_t i = 0; for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p) - cenv.def(checked_cast(*p), *p, checked_cast(protT->at(i++)), &*a); + cenv.def((*p)->as(), *p, protT->at(i++)->as(), &*a); // Write function body try { @@ -286,7 +275,7 @@ AClosure::liftCall(CEnv& cenv, const vector& argsT) const_iterator p = prot()->begin(); size_t i = 0; for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p) - cenv.def(checked_cast(*p), *p, checked_cast(protT->at(i++)), &*a); + cenv.def((*p)->as(), *p, protT->at(i++)->as(), &*a); // Write function body try { @@ -347,7 +336,7 @@ ACall::lift(CEnv& cenv) cenv.push(); for (size_t i = 1; i < size(); ++i) - cenv.def(checked_cast(c->prot()->at(i-1)), at(i), cenv.type(at(i)), NULL); + cenv.def(c->prot()->at(i-1)->as(), at(i), cenv.type(at(i)), NULL); c->liftCall(cenv, argsT); // Lift called closure cenv.pop(); // Restore environment @@ -382,10 +371,10 @@ ACall::compile(CEnv& cenv) void ADefinition::lift(CEnv& cenv) { - if (cenv.code.lookup(checked_cast(at(1)))) + if (cenv.code.lookup(at(1)->as())) throw Error(string("`") + at(1)->str() + "' redefined", loc); // Define first for recursion - cenv.def(checked_cast(at(1)), at(2), cenv.type(at(2)), NULL); + cenv.def(at(1)->as(), at(2), cenv.type(at(2)), NULL); at(2)->lift(cenv); } diff --git a/tuplr.hpp b/tuplr.hpp index 130307e..506742a 100644 --- a/tuplr.hpp +++ b/tuplr.hpp @@ -153,6 +153,11 @@ struct AST { virtual void constrain(TEnv& tenv, Constraints& c) const {} virtual void lift(CEnv& cenv) {} string str() const { ostringstream ss; ss << this; return ss.str(); } + template T as() { + T t = dynamic_cast(this); + if (!t) throw Error("internal error: bad cast", loc); + return t; + } Cursor loc; private: friend class CEnv; @@ -448,7 +453,7 @@ struct Subst : public map { }; /// Type-Time Environment -struct TEnv : public Env { +struct TEnv : public Env { TEnv(PEnv& p) : penv(p), varID(1) {} AType* fresh(const ASymbol* sym) { diff --git a/typing.cpp b/typing.cpp index d6f7acc..105140e 100644 --- a/typing.cpp +++ b/typing.cpp @@ -110,7 +110,7 @@ AClosure::constrain(TEnv& tenv, Constraints& c) const genericType = new AType(loc, tenv.penv.sym("Fn"), tsubst.apply(protT), tsubst.apply(bodyT), 0); tenv.genericTypes.insert(make_pair(this, genericType)); - tenv.def(this, genericType); + //tenv.def(this, genericType); tenv.pop(); subst = new Subst(tsubst); @@ -141,7 +141,7 @@ ADefinition::constrain(TEnv& tenv, Constraints& c) const if (!dynamic_cast(at(1))) throw Error("`def' name is not a symbol", loc); AType* tvar = tenv.var(at(2)); - tenv.def(at(1), tvar); + tenv.def(at(1)->as(), tvar); at(2)->constrain(tenv, c); c.constrain(tenv, this, tvar); } -- cgit v1.2.1