From 1f488a7bd89d4cef07bd41ab22a290b0e230172d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 4 Dec 2010 23:12:06 +0000 Subject: More const-correctness (remove all use of const_cast from lift.cpp). git-svn-id: http://svn.drobilla.net/resp/resp@296 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/lift.cpp | 72 ++++++++++++++++++++++++++++-------------------------------- src/resp.hpp | 20 ++++++++--------- 2 files changed, 43 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/lift.cpp b/src/lift.cpp index 5b3f871..10c4707 100644 --- a/src/lift.cpp +++ b/src/lift.cpp @@ -27,7 +27,7 @@ using namespace std; -static AST* +static const AST* lift_symbol(CEnv& cenv, Code& code, const ASymbol* sym) throw() { const std::string& cppstr = sym->cppstr; @@ -45,15 +45,15 @@ lift_symbol(CEnv& cenv, Code& code, const ASymbol* sym) throw() new ALiteral(T_INT32, index, Cursor()), NULL); } else { - return const_cast(sym); + return sym; } } -static AST* +static const AST* lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() { - List impl; - impl.push_back(const_cast(fn->head())); + List impl; + impl.push_back(fn->head()); const string fnName = cenv.name(fn); const string nameBase = cenv.penv.gensymstr(((fnName != "") ? fnName : "fn").c_str()); @@ -68,21 +68,19 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() AType::const_iterator tp = type->prot()->begin(); List implProtT; - List implProt; + List implProt; // Prepend closure parameter - implProt.push_back(const_cast(cenv.penv.sym("_me"))); + implProt.push_back(cenv.penv.sym("_me")); for (ATuple::const_iterator p = fn->prot()->begin(); p != fn->prot()->end(); ++p) { const AType* paramType = (*tp++)->as_type(); if (paramType->kind == AType::EXPR && *paramType->head() == *cenv.tenv.Fn) { - AType* fnType = new AType(const_cast(cenv.tenv.var()), - const_cast(paramType), - fnType->loc); + const AType* fnType = new AType(cenv.tenv.var(), paramType, fnType->loc); paramType = tup((*p)->loc, cenv.tenv.Tup, fnType, NULL); } cenv.def((*p)->as_symbol(), *p, paramType, NULL); - implProt.push_back(const_cast(*p)); + implProt.push_back(*p); implProtT.push_back(new AType(*paramType)); } @@ -92,7 +90,7 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() const AType* implRetT = NULL; for (ATuple::const_iterator i = fn->iter_at(2); i != fn->end(); ++i) { const AST* lifted = resp_lift(cenv, code, *i); - impl.push_back(const_cast(lifted)); + impl.push_back(lifted); implRetT = cenv.type(lifted); } @@ -106,21 +104,21 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() TList implT; // Type of the implementation function TList tupT(fn->loc, cenv.tenv.Tup, cenv.tenv.var(), NULL); TList consT; - List cons(fn->loc, cenv.penv.sym("Closure"), implName, NULL); + List cons(fn->loc, cenv.penv.sym("Closure"), implName, NULL); const CEnv::FreeVars& freeVars = cenv.liftStack.top(); for (CEnv::FreeVars::const_iterator i = freeVars.begin(); i != freeVars.end(); ++i) { - cons.push_back(const_cast(*i)); - tupT.push_back(const_cast(cenv.type(*i))); - consT.push_back(const_cast(cenv.type(*i))); + cons.push_back(*i); + tupT.push_back(cenv.type(*i)); + consT.push_back(cenv.type(*i)); } cenv.liftStack.pop(); implProtT.push_front(tupT); - implT.push_back(const_cast((AType*)type->head())); - implT.push_back(const_cast(implProtT.head)); - implT.push_back(const_cast(implRetT)); + implT.push_back((AType*)type->head()); + implT.push_back(implProtT.head); + implT.push_back(implRetT); consT.push_front(implT.head); consT.push_front(cenv.tenv.Tup); @@ -135,14 +133,14 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() return cons; } -static AST* +static const AST* lift_call(CEnv& cenv, Code& code, const ATuple* call) throw() { - List copy; + List copy; // Lift all children (callee and arguments, recursively) for (ATuple::const_iterator i = call->begin(); i != call->end(); ++i) - copy.push_back(const_cast(resp_lift(cenv, code, *i))); + copy.push_back(resp_lift(cenv, code, *i)); copy.head->loc = call->loc; @@ -162,9 +160,9 @@ lift_call(CEnv& cenv, Code& code, const ATuple* call) throw() * closure as the first parameter: * (_impl (Fn _impl ...) ...) */ - const ATuple* closure = copy.head->list_ref(0)->as_tuple(); - ASymbol* implSym = const_cast(closure->list_ref(1)->as_symbol()); - const AType* implT = cenv.type(cenv.resolve(implSym)); + const ATuple* closure = copy.head->list_ref(0)->as_tuple(); + const ASymbol* implSym = closure->list_ref(1)->as_symbol(); + const AType* implT = cenv.type(cenv.resolve(implSym)); copy.push_front(implSym); copyT = implT->list_ref(2)->as_type(); } else { @@ -184,7 +182,7 @@ lift_call(CEnv& cenv, Code& code, const ATuple* call) throw() return copy; } -static AST* +static const AST* lift_def(CEnv& cenv, Code& code, const ATuple* def) throw() { // Define stub first for recursion @@ -195,14 +193,13 @@ lift_def(CEnv& cenv, Code& code, const ATuple* def) throw() cenv.setName(body->as_tuple(), sym->str()); assert(def->list_ref(1)->to_symbol()); - List copy; - copy.push_back(const_cast(def->head())); - copy.push_back(const_cast(resp_lift(cenv, code, def->list_ref(1)))); + List copy; + copy.push_back(def->head()); + copy.push_back(resp_lift(cenv, code, def->list_ref(1))); for (ATuple::const_iterator t = def->iter_at(2); t != def->end(); ++t) - copy.push_back(const_cast(resp_lift(cenv, code, *t))); + copy.push_back(resp_lift(cenv, code, *t)); - cenv.setTypeSameAs(const_cast(static_cast(copy.head)), - const_cast(static_cast(def))); + cenv.setTypeSameAs(copy.head, def); if (copy.head->list_ref(1) == copy.head->list_ref(2)) return NULL; // Definition created by lift_fn when body was lifted @@ -214,18 +211,17 @@ lift_def(CEnv& cenv, Code& code, const ATuple* def) throw() return copy; } -static AST* +static const AST* lift_builtin_call(CEnv& cenv, Code& code, const ATuple* call) throw() { - List copy; - copy.push_back(const_cast(call->head())); + List copy; + copy.push_back(call->head()); // Lift all arguments for (ATuple::const_iterator i = call->iter_at(1); i != call->end(); ++i) - copy.push_back(const_cast(resp_lift(cenv, code, *i))); + copy.push_back(resp_lift(cenv, code, *i)); - cenv.setTypeSameAs(const_cast(static_cast(copy.head)), - const_cast(static_cast(call))); + cenv.setTypeSameAs(copy.head, call); return copy; } diff --git a/src/resp.hpp b/src/resp.hpp index 2756f23..5da4214 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -307,11 +307,11 @@ private: struct ATuple : public AST { ATuple(Cursor c) : AST(T_TUPLE, c), _len(0), _vec(0) {} ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _len(exp._len) { - _vec = (AST**)malloc(sizeof(AST*) * _len); + _vec = (const AST**)malloc(sizeof(AST*) * _len); memcpy(_vec, exp._vec, sizeof(AST*) * _len); } - ATuple(AST* first, AST* rest, Cursor c=Cursor()) : AST(T_TUPLE, c), _len(2) { - _vec = (AST**)malloc(sizeof(AST*) * _len); + ATuple(const AST* first, const AST* rest, Cursor c=Cursor()) : AST(T_TUPLE, c), _len(2) { + _vec = (const AST**)malloc(sizeof(AST*) * _len); _vec[0] = first; _vec[1] = rest; } @@ -319,7 +319,7 @@ struct ATuple : public AST { if (!ast) return; _len = 2; - _vec = (AST**)malloc(sizeof(AST*) * _len); + _vec = (const AST**)malloc(sizeof(AST*) * _len); _vec[0] = ast; _vec[1] = NULL; @@ -332,9 +332,7 @@ struct ATuple : public AST { } const AST* head() const { assert(_len > 0); return _vec[0]; } - AST*& head() { assert(_len > 0); return _vec[0]; } const AST* last() const { return _vec[_len - 1]; } - AST*& last() { return _vec[_len - 1]; } bool empty() const { return _len == 0; } size_t tup_len() const { return _len; } @@ -410,8 +408,8 @@ struct ATuple : public AST { private: friend class GC; - size_t _len; - AST** _vec; + size_t _len; + const AST** _vec; }; static bool @@ -438,7 +436,7 @@ struct AType : public ATuple { 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); } - AType(AST* first, AST* rest, Cursor c) : ATuple(first, rest, c), kind(EXPR), id(0) { tag(T_TYPE); } + AType(const AST* first, const AST* rest, Cursor c) : ATuple(first, rest, c), kind(EXPR), id(0) { tag(T_TYPE); } AType(const AType& copy) : ATuple(copy), kind(copy.kind), id(copy.id) { tag(T_TYPE); } bool concrete() const { @@ -495,7 +493,7 @@ struct List { CT* tail; }; -typedef List TList; +typedef List TList; inline bool list_equals(const ATuple* lhs, const ATuple* rhs) @@ -815,7 +813,7 @@ struct CEnv { tenv.vars.insert(make_pair(ast, tvar)); tsubst.add(tvar, type); } - void setTypeSameAs(AST* ast, AST* typedAst) { + void setTypeSameAs(const AST* ast, const AST* typedAst) { tenv.vars.insert(make_pair(ast, tenv.vars[typedAst])); } -- cgit v1.2.1