bool Inst::CastTo(CodeContext& context, RValue& value, SType* type, bool upcast) { auto valueType = value.stype(); if (type == valueType) { // non-assignment and non-comparison operations with enum types // must result in an int type to keep enum variables within range. if (upcast && valueType->isEnum()) value.castToSubtype(); return false; } else if (type->isComplex() || valueType->isComplex()) { context.addError("can not cast complex types"); return true; } else if (type->isPointer()) { if (value.isNullPtr()) { // NOTE: discard current null value and create // a new one using the right type value = RValue::getNullPtr(context, type); return false; } else if (type->subType()->isVoid()) { value = RValue(new BitCastInst(value, *type, "", context), type); return false; } context.addError("can't cast value to pointer type"); return true; } else if (type->isVec()) { // unwrap enum type if (valueType->isEnum()) valueType = value.castToSubtype(); if (valueType->isNumeric()) { CastTo(context, value, type->subType(), upcast); auto i32 = SType::getInt(context, 32); auto mask = RValue::getZero(context, SType::getVec(context, i32, type->size())); auto udef = UndefValue::get(*SType::getVec(context, type->subType(), 1)); auto instEle = InsertElementInst::Create(udef, value, RValue::getZero(context, i32), "", context); auto retVal = new ShuffleVectorInst(instEle, udef, mask, "", context); value = RValue(retVal, type); return false; } else if (valueType->isPointer()) { context.addError("can not cast pointer to vec type"); return true; } else if (type->size() != valueType->size()) { context.addError("can not cast vec types of different sizes"); return true; } else if (type->subType()->isBool()) { // cast to bool is value != 0 auto pred = getPredicate(ParserBase::TT_NEQ, valueType, context); auto op = valueType->subType()->isFloating()? Instruction::FCmp : Instruction::ICmp; auto val = CmpInst::Create(op, pred, value, RValue::getZero(context, valueType), "", context); value = RValue(val, type); return false; } else { auto op = getCastOp(valueType->subType(), type->subType()); auto val = CastInst::Create(op, value, *type, "", context); value = RValue(val, type); return false; } } else if (type->isEnum()) { // casting to enum would violate value constraints context.addError("can't cast to enum type"); return true; } else if (type->isBool()) { if (valueType->isVec()) { context.addError("can not cast vec type to bool"); return true; } else if (valueType->isEnum()) { valueType = value.castToSubtype(); } // cast to bool is value != 0 auto pred = getPredicate(ParserBase::TT_NEQ, valueType, context); auto op = valueType->isFloating()? Instruction::FCmp : Instruction::ICmp; auto val = CmpInst::Create(op, pred, value, RValue::getZero(context, valueType), "", context); value = RValue(val, type); return false; } if (valueType->isPointer()) { context.addError("can't cast pointer to numeric type"); return true; } // unwrap enum type if (valueType->isEnum()) valueType = value.castToSubtype(); auto op = getCastOp(valueType, type); auto val = op != Instruction::AddrSpaceCast? CastInst::Create(op, value, *type, "", context) : value; value = RValue(val, type); return false; }