Allow implicitly converting IntLiteralExpr to other int types
This commit is contained in:
parent
2695a83ac8
commit
810dd3595e
@ -42,8 +42,15 @@ namespace AST {
|
||||
class IntLiteralExpression : public Expression {
|
||||
private:
|
||||
int m_value;
|
||||
std::shared_ptr<types::Type> m_ty;
|
||||
public:
|
||||
IntLiteralExpression(token::Metadata meta, int value) : Expression{ meta }, m_value{ value } {}
|
||||
IntLiteralExpression(token::Metadata meta, int value)
|
||||
: Expression{ meta }
|
||||
, m_value{ value }
|
||||
, m_ty{ { std::shared_ptr<types::Type>{
|
||||
new types::FundamentalType{types::FundamentalTypeKind::Int}
|
||||
} } } {
|
||||
}
|
||||
virtual ~IntLiteralExpression() override = default;
|
||||
virtual std::string formatted() override;
|
||||
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||
|
||||
@ -19,11 +19,10 @@ namespace AST {
|
||||
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) {
|
||||
auto ty = builder.builder->getInt32Ty();
|
||||
|
||||
auto stack_type = new types::FundamentalType{ types::FundamentalTypeKind::Int };
|
||||
|
||||
return codegen::StackValue{
|
||||
llvm::ConstantInt::get(ty, this->m_value),
|
||||
std::unique_ptr<types::Type>{stack_type}
|
||||
this->m_ty,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -43,11 +43,24 @@ namespace AST {
|
||||
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
||||
typecheck::State&,
|
||||
typecheck::Scope&,
|
||||
std::optional<std::shared_ptr<types::Type>>
|
||||
std::optional<std::shared_ptr<types::Type>> expected_ty
|
||||
) {
|
||||
return std::shared_ptr<types::Type>{
|
||||
new types::FundamentalType{ types::FundamentalTypeKind::Int }
|
||||
};
|
||||
// Allow implicitly converting IntLiteralExpression to other types
|
||||
// representable by integers.
|
||||
if (expected_ty) {
|
||||
if ((*expected_ty)->m_kind == types::TypeKind::Fundamental) {
|
||||
auto ty = dynamic_cast<types::FundamentalType*>((*expected_ty).get());
|
||||
if (
|
||||
ty->m_ty == types::FundamentalTypeKind::Bool
|
||||
|| ty->m_ty == types::FundamentalTypeKind::Char
|
||||
|| ty->m_ty == types::FundamentalTypeKind::Int
|
||||
) {
|
||||
this->m_ty = *expected_ty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this->m_ty;
|
||||
}
|
||||
|
||||
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user