aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-12-04 23:21:09 +0000
committerDavid Robillard <d@drobilla.net>2010-12-04 23:21:09 +0000
commit8905a0e25858a047e0844c55ed8a025153ab25d9 (patch)
tree1ba8ae460e27d3c8f91db2178073e56ec1534dd5
parent1f488a7bd89d4cef07bd41ab22a290b0e230172d (diff)
downloadresp-8905a0e25858a047e0844c55ed8a025153ab25d9.tar.gz
resp-8905a0e25858a047e0844c55ed8a025153ab25d9.tar.bz2
resp-8905a0e25858a047e0844c55ed8a025153ab25d9.zip
More const-correctness (remove all use of const_cast).
git-svn-id: http://svn.drobilla.net/resp/resp@297 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r--src/compile.cpp4
-rw-r--r--src/constrain.cpp18
-rw-r--r--src/llvm.cpp2
-rw-r--r--src/parse.cpp38
-rw-r--r--src/repl.cpp6
-rw-r--r--src/resp.cpp2
-rw-r--r--src/resp.hpp14
-rw-r--r--src/unify.cpp6
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<ASymbol*>(cons->head()->as_symbol()), NULL, Cursor());
+ AType* type = new AType(cons->head()->as_symbol(), NULL, Cursor());
TList tlist(type);
vector<CVal> fields;
for (ATuple::const_iterator i = cons->iter_at(1); i != cons->end(); ++i) {
- tlist.push_back(const_cast<AType*>(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<AType*>(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<ASymbol*>(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<ASymbol*>(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<AType>(call->loc, const_cast<AType*>(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<AType*>(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<AType>(call->loc, const_cast<AType*>(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<AType*>(tenv.var()));
- objT.push_back(const_cast<AType*>(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<AType*>(tenv.var(*i)));
+ argsT.push_back(tenv.var(*i));
argsT.head->loc = call->loc;
c.constrain(tenv, head, tup<AType>(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<AType>(Cursor(), const_cast<ASymbol*>(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<ATuple, AST> ret;
+ List<ATuple, const AST> 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<AST*>(exp);
+ return exp;
} else {
// (def (f x) y) => (def f (fn (x) y))
const ATuple* pat = arg1->to_tuple();
- List<ATuple, AST> argsExp;
+ List<ATuple, const AST> argsExp;
ATuple::const_iterator j = pat->begin();
for (++j; j != pat->end(); ++j)
- argsExp.push_back(const_cast<AST*>(*j));
+ argsExp.push_back(*j);
argsExp.head->loc = exp->loc;
const AST* body = *(++i);
- List<ATuple, AST> fnExp;
+ List<ATuple, const AST> fnExp;
fnExp.push_back(new ALexeme(exp->loc, "fn"));
fnExp.push_back(argsExp.head);
for (; i != tup->end(); ++i)
- fnExp.push_back(const_cast<AST*>(*i));
+ fnExp.push_back(*i);
fnExp.head->loc = body->loc;
- List<ATuple, AST> ret;
- ret.push_back(const_cast<AST*>(tup->head()));
- ret.push_back(const_cast<AST*>(pat->head()));
+ List<ATuple, const AST> 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<bool>(T_BOOL, *reinterpret_cast<bool*>(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<ATuple, AST> ret(new ATuple(penv.sym("fn"), NULL, Cursor()));
+ const ATuple* prot = parseTuple(penv, (*a++)->to_tuple());
+ List<ATuple, const AST> 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<AST*>(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<AST*>::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<std::streamsize>::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<const string, ASymbol*> {
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<const string, Handler> aHandlers; ///< Atom parse functions
map<const string, Handler> lHandlers; ///< List parse functions
@@ -600,7 +600,7 @@ struct PEnv : private map<const string, ASymbol*> {
return sym;
}
}
- AST* parse(const AST* exp);
+ const AST* parse(const AST* exp);
typedef std::set<std::string> Primitives;
Primitives primitives;
@@ -636,7 +636,7 @@ struct Subst : public list<Constraint> {
if (in->kind == AType::EXPR) {
TList out;
for (ATuple::const_iterator i = in->begin(); i != in->end(); ++i)
- out.push_back(const_cast<AType*>(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<Constraint> {
if (i != end()) {
const AType* out = i->second->as_type();
if (out->kind == AType::EXPR && !out->concrete())
- out = const_cast<AType*>(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<AST*> Code;
+typedef list<const AST*> 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<AType*>(substitute(elem, from, to)));
+ ret.push_back(substitute(elem, from, to));
else
- ret.push_back(const_cast<AType*>(elem));
+ ret.push_back(elem);
} else {
- ret.push_back(const_cast<AType*>((*i)->as_type()));
+ ret.push_back((*i)->as_type());
}
}
return ret.head;