Permalink
Browse files

Prefer C++-style casts.

This is based on Item 2 of "More Effective C++". In general, the C++ cast operators are more expressive and easy to find,
e.g. by grep. Using const_cast also shuts up some compiler warnings.
  • Loading branch information...
1 parent 9dad0a6 commit 2dbee75523112ad6fdc988fe5c146b4a5c265be9 @redstar redstar committed Aug 2, 2012
Showing with 227 additions and 227 deletions.
  1. +1 −1 gen/aa.cpp
  2. +2 −2 gen/abi-x86-64.cpp
  3. +16 −16 gen/arrays.cpp
  4. +3 −3 gen/asmstmt.cpp
  5. +10 −10 gen/classes.cpp
  6. +7 −7 gen/declarations.cpp
  7. +10 −10 gen/functions.cpp
  8. +23 −23 gen/llvmhelpers.cpp
  9. +4 −4 gen/module.cpp
  10. +10 −10 gen/naked.cpp
  11. +1 −1 gen/nested.cpp
  12. +6 −6 gen/pragma.cpp
  13. +1 −1 gen/rttibuilder.cpp
  14. +15 −15 gen/statements.cpp
  15. +6 −6 gen/structs.cpp
  16. +12 −12 gen/tocall.cpp
  17. +8 −8 gen/todebug.cpp
  18. +66 −66 gen/toir.cpp
  19. +7 −7 gen/tollvm.cpp
  20. +17 −17 gen/typinf.cpp
  21. +2 −2 gen/warnings.cpp
View
@@ -20,7 +20,7 @@ static LLValue* to_keyti(DValue* aa)
{
// keyti param
assert(aa->type->toBasetype()->ty == Taarray);
- TypeAArray * aatype = (TypeAArray*)aa->type->toBasetype();
+ TypeAArray * aatype = static_cast<TypeAArray*>(aa->type->toBasetype());
return DtoTypeInfoOf(aatype->index, false);
}
#else
View
@@ -161,9 +161,9 @@ namespace {
}
}
} else if (ty->ty == Tstruct) {
- Array* fields = &((TypeStruct*) ty)->sym->fields;
+ Array* fields = &static_cast<TypeStruct*>(ty)->sym->fields;
for (size_t i = 0; i < fields->dim; i++) {
- VarDeclaration* field = (VarDeclaration*) fields->data[i];
+ VarDeclaration* field = static_cast<VarDeclaration*>(fields->data[i]);
classifyType(accum, field->type, offset + field->offset);
}
} else {
View
@@ -81,7 +81,7 @@ LLArrayType* DtoStaticArrayType(Type* t)
{
t = t->toBasetype();
assert(t->ty == Tsarray);
- TypeSArray* tsa = (TypeSArray*)t;
+ TypeSArray* tsa = static_cast<TypeSArray*>(t);
Type* tnext = tsa->nextOf();
LLType* elemty = DtoType(tnext);
@@ -196,7 +196,7 @@ bool arrayNeedsPostblit(Type *t)
{
t = DtoArrayElementType(t);
if (t->ty == Tstruct)
- return ((TypeStruct *)t)->sym->postblit != 0;
+ return static_cast<TypeStruct *>(t)->sym->postblit != 0;
return false;
}
@@ -270,8 +270,8 @@ LLType* DtoConstArrayInitializerType(ArrayInitializer* arrinit)
if (arrty->ty != Tsarray)
return DtoType(arrinit->type);
- TypeSArray* tsa = (TypeSArray*)arrty;
- size_t arrlen = (size_t)tsa->dim->toInteger();
+ TypeSArray* tsa = static_cast<TypeSArray*>(arrty);
+ size_t arrlen = static_cast<size_t>(tsa->dim->toInteger());
// get elem type
Type* elemty = arrty->nextOf();
@@ -295,15 +295,15 @@ LLType* DtoConstArrayInitializerType(ArrayInitializer* arrinit)
for (size_t i = 0; i < arrinit->index.dim; i++)
{
// get index
- Expression* idx = (Expression*)arrinit->index.data[i];
+ Expression* idx = static_cast<Expression*>(arrinit->index.data[i]);
// idx can be null, then it's just the next element
if (idx)
j = idx->toInteger();
assert(j < arrlen);
// get value
- Initializer* val = (Initializer*)arrinit->value.data[i];
+ Initializer* val = static_cast<Initializer*>(arrinit->value.data[i]);
assert(val);
LLType* c = DtoConstInitializerType(elemty, val);
@@ -355,8 +355,8 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit)
// initialized elements in the value/index lists
if (arrty->ty == Tsarray)
{
- TypeSArray* tsa = (TypeSArray*)arrty;
- arrlen = (size_t)tsa->dim->toInteger();
+ TypeSArray* tsa = static_cast<TypeSArray*>(arrty);
+ arrlen = static_cast<size_t>(tsa->dim->toInteger());
}
// make sure the number of initializers is sane
@@ -381,15 +381,15 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit)
for (size_t i = 0; i < arrinit->index.dim; i++)
{
// get index
- Expression* idx = (Expression*)arrinit->index.data[i];
+ Expression* idx = static_cast<Expression*>(arrinit->index.data[i]);
// idx can be null, then it's just the next element
if (idx)
j = idx->toInteger();
assert(j < arrlen);
// get value
- Initializer* val = (Initializer*)arrinit->value.data[i];
+ Initializer* val = static_cast<Initializer*>(arrinit->value.data[i]);
assert(val);
// error check from dmd
@@ -540,7 +540,7 @@ static bool isInitialized(Type* et) {
// If it's a typedef with "= void" initializer then don't initialize.
if (et->ty == Ttypedef) {
Logger::println("Typedef: %s", et->toChars());
- TypedefDeclaration* tdd = ((TypeTypedef*)et)->sym;
+ TypedefDeclaration* tdd = static_cast<TypeTypedef*>(et)->sym;
if (tdd && tdd->init && tdd->init->isVoidInitializer())
return false;
}
@@ -863,11 +863,11 @@ DSliceValue* DtoCatArrays(Type* arrayType, Expression* exp1, Expression* exp2)
fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_arraycatnT");
args.push_back(DtoSlicePtr(exp2->toElem(gIR)));
- CatExp *ce = (CatExp*)exp1;
+ CatExp *ce = static_cast<CatExp*>(exp1);
do
{
args.push_back(DtoSlicePtr(ce->e2->toElem(gIR)));
- ce = (CatExp *)ce->e1;
+ ce = static_cast<CatExp *>(ce->e1);
} while (ce->op == TOKcat);
args.push_back(DtoSlicePtr(ce->toElem(gIR)));
@@ -1234,7 +1234,7 @@ LLValue* DtoArrayLen(DValue* v)
assert(!v->isSlice());
assert(!v->isNull());
assert(v->type->toBasetype()->ty == Tsarray);
- TypeSArray *sarray = (TypeSArray*)v->type->toBasetype();
+ TypeSArray *sarray = static_cast<TypeSArray*>(v->type->toBasetype());
return DtoConstSize_t(sarray->dim->toUInteger());
}
assert(0 && "unsupported array for len");
@@ -1317,7 +1317,7 @@ DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
fatal();
}
- uinteger_t len = ((TypeSArray*)fromtype)->dim->toUInteger();
+ uinteger_t len = static_cast<TypeSArray*>(fromtype)->dim->toUInteger();
rval2 = LLConstantInt::get(DtoSize_t(), len, false);
if (fromtype->nextOf()->size() != totype->nextOf()->size())
rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
@@ -1337,7 +1337,7 @@ DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
if (Logger::enabled())
Logger::cout() << "to sarray" << '\n';
- size_t tosize = ((TypeSArray*)totype)->dim->toInteger();
+ size_t tosize = static_cast<TypeSArray*>(totype)->dim->toInteger();
if (fromtype->ty == Tsarray) {
LLValue* uval = u->getRVal();
View
@@ -528,7 +528,7 @@ void AsmBlockStatement::toIR(IRState* p)
// do asm statements
for (unsigned i=0; i<statements->dim; i++)
{
- Statement* s = (Statement*)statements->data[i];
+ Statement* s = static_cast<Statement*>(statements->data[i]);
if (s) {
s->toIR(p);
}
@@ -800,7 +800,7 @@ Statement *AsmBlockStatement::syntaxCopy()
a->setDim(statements->dim);
for (size_t i = 0; i < statements->dim; i++)
{
- Statement *s = (Statement *)statements->data[i];
+ Statement *s = static_cast<Statement *>(statements->data[i]);
if (s)
s = s->syntaxCopy();
a->data[i] = s;
@@ -842,7 +842,7 @@ void AsmBlockStatement::toNakedIR(IRState *p)
// do asm statements
for (unsigned i=0; i<statements->dim; i++)
{
- Statement* s = (Statement*)statements->data[i];
+ Statement* s = static_cast<Statement*>(statements->data[i]);
if (s) s->toNakedIR(p);
}
}
View
@@ -272,11 +272,11 @@ DValue* DtoCastClass(DValue* val, Type* _to)
// must be class/interface
assert(to->ty == Tclass);
- TypeClass* tc = (TypeClass*)to;
+ TypeClass* tc = static_cast<TypeClass*>(to);
// from type
Type* from = val->getType()->toBasetype();
- TypeClass* fc = (TypeClass*)from;
+ TypeClass* fc = static_cast<TypeClass*>(from);
// x -> interface
if (InterfaceDeclaration* it = tc->sym->isInterfaceDeclaration()) {
@@ -372,7 +372,7 @@ DValue* DtoDynamicCastObject(DValue* val, Type* _to)
assert(funcTy->getParamType(0) == obj->getType());
// ClassInfo c
- TypeClass* to = (TypeClass*)_to->toBasetype();
+ TypeClass* to = static_cast<TypeClass*>(_to->toBasetype());
to->sym->codegen(Type::sir);
LLValue* cinfo = to->sym->ir.irStruct->getClassInfoSymbol();
@@ -436,7 +436,7 @@ DValue* DtoDynamicCastInterface(DValue* val, Type* _to)
ptr = DtoBitCast(ptr, funcTy->getParamType(0));
// ClassInfo c
- TypeClass* to = (TypeClass*)_to->toBasetype();
+ TypeClass* to = static_cast<TypeClass*>(_to->toBasetype());
to->sym->codegen(Type::sir);
LLValue* cinfo = to->sym->ir.irStruct->getClassInfoSymbol();
// unfortunately this is needed as the implementation of object differs somehow from the declaration
@@ -638,7 +638,7 @@ static unsigned build_classinfo_flags(ClassDeclaration* cd)
continue;
for (size_t i = 0; i < cd2->members->dim; i++)
{
- Dsymbol *sm = (Dsymbol *)cd2->members->data[i];
+ Dsymbol *sm = static_cast<Dsymbol *>(cd2->members->data[i]);
if (sm->isVarDeclaration() && !sm->isVarDeclaration()->isDataseg()) // is this enough?
hasOffTi = true;
//printf("sm = %s %s\n", sm->kind(), sm->toChars());
@@ -685,7 +685,7 @@ LLConstant* DtoDefineClassInfo(ClassDeclaration* cd)
LOG_SCOPE;
assert(cd->type->ty == Tclass);
- TypeClass* cdty = (TypeClass*)cd->type;
+ TypeClass* cdty = static_cast<TypeClass*>(cd->type);
IrStruct* ir = cd->ir.irStruct;
assert(ir);
@@ -757,7 +757,7 @@ LLConstant* DtoDefineClassInfo(ClassDeclaration* cd)
b.push(build_class_dtor(cd));
// invariant
- VarDeclaration* invVar = (VarDeclaration*)cinfo->fields.data[6];
+ VarDeclaration* invVar = static_cast<VarDeclaration*>(cinfo->fields.data[6]);
b.push_funcptr(cd->inv, invVar->type);
// uint flags
@@ -770,7 +770,7 @@ LLConstant* DtoDefineClassInfo(ClassDeclaration* cd)
b.push_funcptr(cd->aggDelete, Type::tvoid->pointerTo());
// offset typeinfo
- VarDeclaration* offTiVar = (VarDeclaration*)cinfo->fields.data[9];
+ VarDeclaration* offTiVar = static_cast<VarDeclaration*>(cinfo->fields.data[9]);
#if GENERATE_OFFTI
@@ -786,13 +786,13 @@ LLConstant* DtoDefineClassInfo(ClassDeclaration* cd)
#endif // GENERATE_OFFTI
// default constructor
- VarDeclaration* defConstructorVar = (VarDeclaration*)cinfo->fields.data[10];
+ VarDeclaration* defConstructorVar = static_cast<VarDeclaration*>(cinfo->fields.data[10]);
b.push_funcptr(cd->defaultCtor, defConstructorVar->type);
#if DMDV2
// xgetMembers
- VarDeclaration* xgetVar = (VarDeclaration*)cinfo->fields.data[11];
+ VarDeclaration* xgetVar = static_cast<VarDeclaration*>(cinfo->fields.data[11]);
b.push_funcptr(cd->findGetMembers(), xgetVar->type);
#else
View
@@ -85,7 +85,7 @@ void TupleDeclaration::codegen(Ir* p)
for (int i=0; i < n; ++i)
{
- DsymbolExp* exp = (DsymbolExp*)objects->data[i];
+ DsymbolExp* exp = static_cast<DsymbolExp*>(objects->data[i]);
assert(exp->op == TOKdsymbol);
exp->s->codegen(p);
}
@@ -262,7 +262,7 @@ void TemplateInstance::codegen(Ir* p)
{
for (unsigned i = 0; i < members->dim; i++)
{
- Dsymbol *s = (Dsymbol *)members->data[i];
+ Dsymbol *s = static_cast<Dsymbol *>(members->data[i]);
s->codegen(p);
}
}
@@ -276,7 +276,7 @@ void TemplateMixin::codegen(Ir* p)
{
for (unsigned i = 0; i < members->dim; i++)
{
- Dsymbol *s = (Dsymbol *)members->data[i];
+ Dsymbol *s = static_cast<Dsymbol *>(members->data[i]);
if (s->isVarDeclaration())
continue;
s->codegen(p);
@@ -293,7 +293,7 @@ void AttribDeclaration::codegen(Ir* p)
if (d)
{
for (unsigned i = 0; i < d->dim; i++)
- { Dsymbol *s = (Dsymbol *)d->data[i];
+ { Dsymbol *s = static_cast<Dsymbol *>(d->data[i]);
s->codegen(p);
}
}
@@ -309,12 +309,12 @@ void PragmaDeclaration::codegen(Ir* p)
{
assert(args && args->dim == 1);
- Expression *e = (Expression *)args->data[0];
+ Expression *e = static_cast<Expression *>(args->data[0]);
assert(e->op == TOKstring);
- StringExp *se = (StringExp *)e;
- char *name = (char *)mem.malloc(se->len + 1);
+ StringExp *se = static_cast<StringExp *>(e);
+ char *name = static_cast<char *>(mem.malloc(se->len + 1));
memcpy(name, se->string, se->len);
name[se->len] = 0;
obj_includelib(name);
View
@@ -35,7 +35,7 @@ llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype,
// sanity check
assert(type->ty == Tfunction);
- TypeFunction* f = (TypeFunction*)type;
+ TypeFunction* f = static_cast<TypeFunction*>(type);
TargetABI* abi = (f->linkage == LINKintrinsic ? TargetABI::getIntrinsic() : gABI);
// Tell the ABI we're resolving a new function type
@@ -219,7 +219,7 @@ llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype,
static llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl)
{
- TypeFunction* f = (TypeFunction*)fdecl->type;
+ TypeFunction* f = static_cast<TypeFunction*>(fdecl->type);
LLFunctionType* fty = 0;
// create new ir funcTy
@@ -317,7 +317,7 @@ void DtoResolveFunction(FuncDeclaration* fdecl)
Type *type = fdecl->type;
// If errors occurred compiling it, such as bugzilla 6118
if (type && type->ty == Tfunction) {
- Type *next = ((TypeFunction *)type)->next;
+ Type *next = static_cast<TypeFunction *>(type)->next;
if (!next || next->ty == Terror)
return;
}
@@ -349,12 +349,12 @@ void DtoResolveFunction(FuncDeclaration* fdecl)
fdecl->llvmInternal = LLVMintrinsic;
DtoOverloadedIntrinsicName(tinst, tempdecl, fdecl->intrinsicName);
fdecl->linkage = LINKintrinsic;
- ((TypeFunction*)fdecl->type)->linkage = LINKintrinsic;
+ static_cast<TypeFunction*>(fdecl->type)->linkage = LINKintrinsic;
}
else if (tempdecl->llvmInternal == LLVMinline_asm)
{
Logger::println("magic inline asm found");
- TypeFunction* tf = (TypeFunction*)fdecl->type;
+ TypeFunction* tf = static_cast<TypeFunction*>(fdecl->type);
if (tf->varargs != 1 || (fdecl->parameters && fdecl->parameters->dim != 0))
{
error("invalid __asm declaration, must be a D style variadic with no explicit parameters");
@@ -469,7 +469,7 @@ void DtoDeclareFunction(FuncDeclaration* fdecl)
// get TypeFunction*
Type* t = fdecl->type->toBasetype();
- TypeFunction* f = (TypeFunction*)t;
+ TypeFunction* f = static_cast<TypeFunction*>(t);
bool declareOnly = !mustDefineSymbol(fdecl);
@@ -600,7 +600,7 @@ void DtoDeclareFunction(FuncDeclaration* fdecl)
if (fdecl->parameters && fdecl->parameters->dim > k)
{
int paramIndex = f->fty.reverseParams ? fdecl->parameters->dim-k-1 : k;
- Dsymbol* argsym = (Dsymbol*)fdecl->parameters->data[paramIndex];
+ Dsymbol* argsym = static_cast<Dsymbol*>(fdecl->parameters->data[paramIndex]);
VarDeclaration* argvd = argsym->isVarDeclaration();
assert(argvd);
@@ -659,7 +659,7 @@ void DtoDefineFunction(FuncDeclaration* fd)
fd->ir.irFunc->diSubprogram = DtoDwarfSubProgram(fd);
Type* t = fd->type->toBasetype();
- TypeFunction* f = (TypeFunction*)t;
+ TypeFunction* f = static_cast<TypeFunction*>(t);
// assert(f->irtype);
llvm::Function* func = fd->ir.irFunc->func;
@@ -749,7 +749,7 @@ void DtoDefineFunction(FuncDeclaration* fd)
assert(n == fd->parameters->dim);
for (int i=0; i < n; ++i)
{
- Dsymbol* argsym = (Dsymbol*)fd->parameters->data[i];
+ Dsymbol* argsym = static_cast<Dsymbol*>(fd->parameters->data[i]);
VarDeclaration* vd = argsym->isVarDeclaration();
assert(vd);
@@ -904,7 +904,7 @@ llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl)
ClassDeclaration* base = cd->baseClass;
if (!base)
break;
- FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type);
+ FuncDeclaration* f2 = base->findFunc(fdecl->ident, static_cast<TypeFunction*>(fdecl->type));
if (f2) {
f = f2;
cd = base;
Oops, something went wrong.

0 comments on commit 2dbee75

Please sign in to comment.