From 8905a0e25858a047e0844c55ed8a025153ab25d9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 4 Dec 2010 23:21:09 +0000 Subject: More const-correctness (remove all use of const_cast). git-svn-id: http://svn.drobilla.net/resp/resp@297 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/compile.cpp | 4 ++-- src/constrain.cpp | 18 +++++++++--------- src/llvm.cpp | 2 +- src/parse.cpp | 38 +++++++++++++++++++------------------- src/repl.cpp | 6 +++--- src/resp.cpp | 2 +- src/resp.hpp | 14 +++++++------- src/unify.cpp | 6 +++--- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/compile.cpp b/src/compile.cpp index 142fa0a..4ac7dfc 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -115,11 +115,11 @@ compile_def(CEnv& cenv, const ATuple* def) throw() static CVal compile_cons(CEnv& cenv, const ATuple* cons) throw() { - AType* type = new AType(const_cast(cons->head()->as_symbol()), NULL, Cursor()); + AType* type = new AType(cons->head()->as_symbol(), NULL, Cursor()); TList tlist(type); vector fields; for (ATuple::const_iterator i = cons->iter_at(1); i != cons->end(); ++i) { - tlist.push_back(const_cast(cenv.type(*i))); + tlist.push_back(cenv.type(*i)); fields.push_back(resp_compile(cenv, *i)); } return cenv.engine()->compileTup(cenv, type, resp_compile(cenv, type), fields); diff --git a/src/constrain.cpp b/src/constrain.cpp index 410df87..e151eb6 100644 --- a/src/constrain.cpp +++ b/src/constrain.cpp @@ -48,7 +48,7 @@ constrain_fn(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) defs.insert(sym); const AType* tvar = tenv.fresh(sym); frame.push_back(make_pair(sym, tvar)); - protT.push_back(const_cast(tvar)); + protT.push_back(tvar); } protT.head->loc = call->loc; @@ -115,11 +115,11 @@ constrain_def_type(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) const ATuple* exp = (*i)->as_tuple(); const ASymbol* tag = (*exp->begin())->as_symbol(); TList consT; - consT.push_back(new AType(const_cast(sym), AType::NAME)); + consT.push_back(new AType(sym, AType::NAME)); for (ATuple::const_iterator i = exp->begin(); i != exp->end(); ++i) { const ASymbol* sym = (*i)->to_symbol(); THROW_IF(!sym, (*i)->loc, "type expression element is not a symbol"); - consT.push_back(new AType(const_cast(sym), AType::NAME)); + consT.push_back(new AType(sym, AType::NAME)); } consT.head->loc = exp->loc; type.push_back(consT); @@ -147,7 +147,7 @@ constrain_match(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) if (!matcheeT) { const AType* headT = consT->head()->as_type(); - matcheeT = tup(call->loc, const_cast(headT), 0); + matcheeT = new AType(headT, 0, call->loc); } THROW_IF(i == call->end(), pattern->loc, "missing pattern body"); @@ -193,7 +193,7 @@ constrain_cons(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) if (sym->cppstr == "Tup") { TList tupT(new AType(tenv.Tup, NULL, call->loc)); for (ATuple::const_iterator i = call->iter_at(1); i != call->end(); ++i) { - tupT.push_back(const_cast(tenv.var(*i))); + tupT.push_back(tenv.var(*i)); } type = tupT; } else { @@ -201,7 +201,7 @@ constrain_cons(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) THROW_IF(!consTRef, call->loc, (format("call to undefined constructor `%1%'") % sym->cppstr).str()); const AType* consT = *consTRef; - type = tup(call->loc, const_cast(consT->head()->as_type()), 0); + type = new AType(consT->head()->as_type(), 0, call->loc); } c.constrain(tenv, call, type); } @@ -223,8 +223,8 @@ constrain_dot(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) TList objT(new AType(tenv.Tup, NULL, call->loc)); for (int i = 0; i < idx->val; ++i) - objT.push_back(const_cast(tenv.var())); - objT.push_back(const_cast(retT)); + objT.push_back(tenv.var()); + objT.push_back(retT); objT.push_back(new AType(obj->loc, AType::DOTS)); c.constrain(tenv, obj, objT); } @@ -259,7 +259,7 @@ constrain_call(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) const AType* retT = tenv.var(call); TList argsT; for (ATuple::const_iterator i = call->iter_at(1); i != call->end(); ++i) - argsT.push_back(const_cast(tenv.var(*i))); + argsT.push_back(tenv.var(*i)); argsT.head->loc = call->loc; c.constrain(tenv, head, tup(head->loc, tenv.Fn, argsT.head, retT, 0)); c.constrain(tenv, call, retT); diff --git a/src/llvm.cpp b/src/llvm.cpp index eabe508..b6dea5d 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -417,7 +417,7 @@ LLVMEngine::compileMatch(CEnv& cenv, const ATuple* match) const AST* pat = *i++; const AST* body = *i++; const ASymbol* sym = pat->to_tuple()->head()->as_symbol(); - const AType* patT = tup(Cursor(), const_cast(sym), 0); + const AType* patT = new AType(sym, 0, Cursor()); Value* typeV = llVal(resp_compile(cenv, patT)); Value* condV = engine->builder.CreateICmp(CmpInst::ICMP_EQ, rtti, typeV); diff --git a/src/parse.cpp b/src/parse.cpp index 7f107aa..8a7e3e0 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -23,16 +23,16 @@ using namespace std; -ATuple* +const ATuple* parseTuple(PEnv& penv, const ATuple* e) { - List ret; + List ret; FOREACHP(ATuple::const_iterator, i, e) ret.push_back(penv.parse(*i)); return ret.head; } -AST* +const AST* PEnv::parse(const AST* exp) { const ATuple* tup = exp->to_tuple(); @@ -86,7 +86,7 @@ PEnv::parse(const AST* exp) * Macro Functions * ***************************************************************************/ -inline AST* +inline const AST* macDef(PEnv& penv, const AST* exp) { const ATuple* tup = exp->to_tuple(); @@ -95,28 +95,28 @@ macDef(PEnv& penv, const AST* exp) const AST* arg1 = *(++i); THROW_IF(i == tup->end(), arg1->loc, "Unexpected end of `def' macro call"); if (arg1->to_lexeme()) { - return const_cast(exp); + return exp; } else { // (def (f x) y) => (def f (fn (x) y)) const ATuple* pat = arg1->to_tuple(); - List argsExp; + List argsExp; ATuple::const_iterator j = pat->begin(); for (++j; j != pat->end(); ++j) - argsExp.push_back(const_cast(*j)); + argsExp.push_back(*j); argsExp.head->loc = exp->loc; const AST* body = *(++i); - List fnExp; + List fnExp; fnExp.push_back(new ALexeme(exp->loc, "fn")); fnExp.push_back(argsExp.head); for (; i != tup->end(); ++i) - fnExp.push_back(const_cast(*i)); + fnExp.push_back(*i); fnExp.head->loc = body->loc; - List ret; - ret.push_back(const_cast(tup->head())); - ret.push_back(const_cast(pat->head())); + List ret; + ret.push_back(tup->head()); + ret.push_back(pat->head()); ret.push_back(fnExp.head); ret.head->loc = exp->loc; return ret.head; @@ -128,26 +128,26 @@ macDef(PEnv& penv, const AST* exp) * Parser Functions * ***************************************************************************/ -inline AST* +inline const AST* parseCall(PEnv& penv, const AST* exp, void* arg) { return parseTuple(penv, exp->to_tuple()); } -inline AST* +inline const AST* parseBool(PEnv& penv, const AST* exp, void* arg) { return new ALiteral(T_BOOL, *reinterpret_cast(arg), exp->loc); } -inline AST* +inline const AST* parseFn(PEnv& penv, const AST* exp, void* arg) { const ATuple* texp = exp->to_tuple(); ATuple::const_iterator a = texp->begin(); THROW_IF(++a == texp->end(), exp->loc, "Unexpected end of `fn' form"); - ATuple* prot = parseTuple(penv, (*a++)->to_tuple()); - List ret(new ATuple(penv.sym("fn"), NULL, Cursor())); + const ATuple* prot = parseTuple(penv, (*a++)->to_tuple()); + List ret(new ATuple(penv.sym("fn"), NULL, Cursor())); ret.push_back(prot); while (a != texp->end()) ret.push_back(penv.parse(*a++)); @@ -155,7 +155,7 @@ parseFn(PEnv& penv, const AST* exp, void* arg) return new ATuple(*ret.head); } -inline AST* +inline const AST* parseQuote(PEnv& penv, const AST* exp, void* arg) { const ATuple* texp = exp->to_tuple(); @@ -190,7 +190,7 @@ initLang(PEnv& penv, TEnv& tenv) penv.reg(false, "#f", PEnv::Handler(parseBool, &falseVal)); // Macros - penv.defmac("def", macDef); + penv.defmac("def", macDef); // Special forms penv.reg(true, "fn", PEnv::Handler(parseFn)); diff --git a/src/repl.cpp b/src/repl.cpp index 7ef7ff3..4ea4d68 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -105,7 +105,7 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) for (Parsed::const_iterator i = parsed.begin(); i != parsed.end(); ++i) { const AST* l = resp_lift(cenv, lifted, *i); if (l) - lifted.push_back(const_cast(l)); + lifted.push_back(l); } if (cenv.args.find("-L") != cenv.args.end()) { @@ -122,7 +122,7 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) val = resp_compile(cenv, call); } else { assert(*i); - ATuple* tup = (*i)->to_tuple(); + const ATuple* tup = (*i)->to_tuple(); if (!tup || (tup->tup_len() > 0)) exprs.push_back(*i); } @@ -135,7 +135,7 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) f = cenv.engine()->startFunction(cenv, "main", new ATuple(cursor), fnT); // Compile expressions (other than function definitions) into it - for (list::const_iterator i = exprs.begin(); i != exprs.end(); ++i) + for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i) val = resp_compile(cenv, *i); // Finish compilation diff --git a/src/resp.cpp b/src/resp.cpp index eb70e62..0d1af5a 100644 --- a/src/resp.cpp +++ b/src/resp.cpp @@ -147,7 +147,7 @@ main(int argc, char** argv) if (!exp || (exp->as_tuple() && exp->as_tuple()->tup_len() == 1)) break; - AST* ast = penv.parse(exp); + const AST* ast = penv.parse(exp); pprint(os, ast, cenv, false); is.ignore(std::numeric_limits::max(), '\n'); // Skip newlines } diff --git a/src/resp.hpp b/src/resp.hpp index 5da4214..6a40729 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -432,7 +432,7 @@ list_contains(const ATuple* head, const AST* child) { /// Type Expression, e.g. "Int", "(Fn (Int Int) Float)" struct AType : public ATuple { enum Kind { VAR, NAME, PRIM, EXPR, DOTS }; - AType(ASymbol* s, Kind k) : ATuple(s, NULL, s->loc), kind(k), id(0) { tag(T_TYPE); } + AType(const ASymbol* s, Kind k) : ATuple(s, NULL, s->loc), kind(k), id(0) { tag(T_TYPE); } AType(Cursor c, unsigned i) : ATuple(c), kind(VAR), id(i) { tag(T_TYPE); } AType(Cursor c, Kind k=EXPR) : ATuple(c), kind(k), id(0) { tag(T_TYPE); } AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) { tag(T_TYPE); } @@ -567,8 +567,8 @@ AST::operator==(const AST& rhs) const /// Parse Time Environment (really just a symbol table) struct PEnv : private map { PEnv() : symID(0) {} - typedef AST* (*PF)(PEnv&, const AST*, void*); ///< Parse Function - typedef AST* (*MF)(PEnv&, const AST*); ///< Macro Function + typedef const AST* (*PF)(PEnv&, const AST*, void*); ///< Parse Function + typedef const AST* (*MF)(PEnv&, const AST*); ///< Macro Function struct Handler { Handler(PF f, void* a=0) : func(f), arg(a) {} PF func; void* arg; }; map aHandlers; ///< Atom parse functions map lHandlers; ///< List parse functions @@ -600,7 +600,7 @@ struct PEnv : private map { return sym; } } - AST* parse(const AST* exp); + const AST* parse(const AST* exp); typedef std::set Primitives; Primitives primitives; @@ -636,7 +636,7 @@ struct Subst : public list { if (in->kind == AType::EXPR) { TList out; for (ATuple::const_iterator i = in->begin(); i != in->end(); ++i) - out.push_back(const_cast(apply((*i)->as_type()))); + out.push_back(apply((*i)->as_type())); out.head->loc = in->loc; return out.head; } else { @@ -644,7 +644,7 @@ struct Subst : public list { if (i != end()) { const AType* out = i->second->as_type(); if (out->kind == AType::EXPR && !out->concrete()) - out = const_cast(apply(out->as_type())); + out = apply(out->as_type()); return out; } else { return new AType(*in); @@ -879,7 +879,7 @@ private: * EVAL/REPL/MAIN * ***************************************************************************/ -typedef list Code; +typedef list Code; void pprint(std::ostream& out, const AST* ast, CEnv* cenv, bool types); void initLang(PEnv& penv, TEnv& tenv); diff --git a/src/unify.cpp b/src/unify.cpp index 51179d9..0c9dea2 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -77,11 +77,11 @@ substitute(const AType* tup, const AType* from, const AType* to) } else if (*i != to) { const AType* elem = (*i)->as_type(); if (elem->kind == AType::EXPR) - ret.push_back(const_cast(substitute(elem, from, to))); + ret.push_back(substitute(elem, from, to)); else - ret.push_back(const_cast(elem)); + ret.push_back(elem); } else { - ret.push_back(const_cast((*i)->as_type())); + ret.push_back((*i)->as_type()); } } return ret.head; -- cgit v1.2.1