diff options
author | David Robillard <d@drobilla.net> | 2010-12-04 23:12:06 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2010-12-04 23:12:06 +0000 |
commit | 1f488a7bd89d4cef07bd41ab22a290b0e230172d (patch) | |
tree | b96a87f0b35167ffd3c62f6da3f7da872ee8d7f6 | |
parent | 8ee352054cff3512c8e6dc3fd4738ee24ad267af (diff) | |
download | resp-1f488a7bd89d4cef07bd41ab22a290b0e230172d.tar.gz resp-1f488a7bd89d4cef07bd41ab22a290b0e230172d.tar.bz2 resp-1f488a7bd89d4cef07bd41ab22a290b0e230172d.zip |
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
-rw-r--r-- | src/lift.cpp | 72 | ||||
-rw-r--r-- | src/resp.hpp | 20 |
2 files changed, 43 insertions, 49 deletions
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<int32_t>(T_INT32, index, Cursor()), NULL); } else { - return const_cast<ASymbol*>(sym); + return sym; } } -static AST* +static const AST* lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() { - List<ATuple,AST> impl; - impl.push_back(const_cast<AST*>(fn->head())); + List<ATuple, const AST> 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<AType,AType> implProtT; - List<ATuple,AST> implProt; + List<ATuple, const AST> implProt; // Prepend closure parameter - implProt.push_back(const_cast<ASymbol*>(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<AType*>(cenv.tenv.var()), - const_cast<AType*>(paramType), - fnType->loc); + const AType* fnType = new AType(cenv.tenv.var(), paramType, fnType->loc); paramType = tup<const AType>((*p)->loc, cenv.tenv.Tup, fnType, NULL); } cenv.def((*p)->as_symbol(), *p, paramType, NULL); - implProt.push_back(const_cast<AST*>(*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<AST*>(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<ATuple, AST> cons(fn->loc, cenv.penv.sym("Closure"), implName, NULL); + List<ATuple, const AST> 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<ASymbol*>(*i)); - tupT.push_back(const_cast<AType*>(cenv.type(*i))); - consT.push_back(const_cast<AType*>(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*>((AType*)type->head())); - implT.push_back(const_cast<AType*>(implProtT.head)); - implT.push_back(const_cast<AType*>(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<ATuple, AST> copy; + List<ATuple, const AST> copy; // Lift all children (callee and arguments, recursively) for (ATuple::const_iterator i = call->begin(); i != call->end(); ++i) - copy.push_back(const_cast<AST*>(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<ASymbol*>(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<ATuple, AST> copy; - copy.push_back(const_cast<AST*>(def->head())); - copy.push_back(const_cast<AST*>(resp_lift(cenv, code, def->list_ref(1)))); + List<ATuple, const AST> 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<AST*>(resp_lift(cenv, code, *t))); + copy.push_back(resp_lift(cenv, code, *t)); - cenv.setTypeSameAs(const_cast<AST*>(static_cast<const AST*>(copy.head)), - const_cast<AST*>(static_cast<const AST*>(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<ATuple,AST> copy; - copy.push_back(const_cast<AST*>(call->head())); + List<ATuple, const AST> 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<AST*>(resp_lift(cenv, code, *i))); + copy.push_back(resp_lift(cenv, code, *i)); - cenv.setTypeSameAs(const_cast<AST*>(static_cast<const AST*>(copy.head)), - const_cast<AST*>(static_cast<const AST*>(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<AType, AType> TList; +typedef List<AType, const AType> 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])); } |