Make types in the AST be shared_ptr
This commit is contained in:
parent
e45f120f1c
commit
44c9e31556
16
src/ast.h
16
src/ast.h
@ -148,13 +148,13 @@ namespace AST {
|
||||
|
||||
class InitializationStatement : public Statement {
|
||||
private:
|
||||
std::unique_ptr<types::Type> m_type;
|
||||
std::shared_ptr<types::Type> m_type;
|
||||
std::string m_name;
|
||||
std::optional<std::unique_ptr<Expression>> m_expr;
|
||||
public:
|
||||
InitializationStatement(
|
||||
token::Metadata meta,
|
||||
std::unique_ptr<types::Type> ty,
|
||||
std::shared_ptr<types::Type> ty,
|
||||
std::string name,
|
||||
std::optional<std::unique_ptr<Expression>> expr)
|
||||
: Statement{ meta }
|
||||
@ -211,22 +211,22 @@ namespace AST {
|
||||
|
||||
class Function : public TopLevelStatement {
|
||||
private:
|
||||
std::unique_ptr<types::Type> m_return_ty;
|
||||
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> m_params;
|
||||
std::shared_ptr<types::Type> m_return_ty;
|
||||
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> m_params;
|
||||
bool m_is_vararg;
|
||||
std::string m_name;
|
||||
std::optional<std::vector<std::unique_ptr<Statement>>> m_statements;
|
||||
public:
|
||||
Function(
|
||||
token::Metadata meta,
|
||||
std::unique_ptr<types::Type> return_ty,
|
||||
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params,
|
||||
std::shared_ptr<types::Type> return_ty,
|
||||
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> params,
|
||||
bool is_vararg,
|
||||
std::string name,
|
||||
std::optional<std::vector<std::unique_ptr<Statement>>> statements)
|
||||
: TopLevelStatement{ meta }
|
||||
, m_return_ty{ std::move(return_ty) }
|
||||
, m_params{ std::move(params) }
|
||||
, m_return_ty{ return_ty }
|
||||
, m_params{ params }
|
||||
, m_is_vararg{ is_vararg }
|
||||
, m_name{ name }
|
||||
, m_statements{ std::move(statements) } {
|
||||
|
||||
@ -141,7 +141,7 @@ namespace AST {
|
||||
builder.builder->CreateStore(value.value, ptr, false);
|
||||
}
|
||||
|
||||
scope.values[this->m_name] = codegen::StackValue{ ptr, std::move(this->m_type) };
|
||||
scope.values[this->m_name] = codegen::StackValue{ ptr, this->m_type };
|
||||
}
|
||||
|
||||
void IfStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
@ -181,11 +181,11 @@ namespace AST {
|
||||
|
||||
|
||||
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
std::shared_ptr<types::Type> ret_ty_ptr{ std::move(this->m_return_ty) };
|
||||
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };
|
||||
std::vector<std::shared_ptr<types::Type>> param_ty_ptrs{};
|
||||
|
||||
for (auto& param : this->m_params) {
|
||||
param_ty_ptrs.push_back(std::move(param.second));
|
||||
param_ty_ptrs.push_back(param.second);
|
||||
}
|
||||
|
||||
auto fn_ty_ptr = std::shared_ptr<types::Type>{ new types::FunctionType{ ret_ty_ptr, param_ty_ptrs, this->m_is_vararg } };
|
||||
|
||||
@ -7,7 +7,7 @@ namespace parsing {
|
||||
namespace {
|
||||
Result<std::unique_ptr<AST::Expression>, std::string> parse_expression(token::TokenStream& stream);
|
||||
|
||||
Result<std::unique_ptr<types::Type>, std::string> parse_type(token::TokenStream& stream) {
|
||||
Result<std::shared_ptr<types::Type>, std::string> parse_type(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
auto token = inner.expect(token::Type::Ident);
|
||||
@ -15,19 +15,19 @@ namespace parsing {
|
||||
// TODO eventually make this be potentially more than one word
|
||||
std::string type_name = token.content;
|
||||
|
||||
std::unique_ptr<types::Type> returned{};
|
||||
std::shared_ptr<types::Type> returned{};
|
||||
|
||||
if (type_name == "int") {
|
||||
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int };
|
||||
returned = std::unique_ptr<types::Type>{ ty };
|
||||
returned = std::shared_ptr<types::Type>{ ty };
|
||||
}
|
||||
else if (type_name == "char") {
|
||||
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Char };
|
||||
returned = std::unique_ptr<types::Type>{ ty };
|
||||
returned = std::shared_ptr<types::Type>{ ty };
|
||||
}
|
||||
else if (type_name == "void") {
|
||||
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Void };
|
||||
returned = std::unique_ptr<types::Type>{ ty };
|
||||
returned = std::shared_ptr<types::Type>{ ty };
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Expected type name, got " + type_name);
|
||||
@ -36,7 +36,7 @@ namespace parsing {
|
||||
while (inner.peek().type == token::Type::Symbol && inner.peek().content == "*") {
|
||||
inner.next();
|
||||
auto ty = new types::PointerType{ std::move(returned) };
|
||||
returned = std::unique_ptr<types::Type>{ ty };
|
||||
returned = std::shared_ptr<types::Type>{ ty };
|
||||
}
|
||||
|
||||
|
||||
@ -274,7 +274,7 @@ namespace parsing {
|
||||
auto name_token = inner.expect(token::Type::Ident);
|
||||
inner.expect(token::Type::Symbol, "(");
|
||||
|
||||
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params;
|
||||
std::vector<std::pair<std::optional<std::string>, std::shared_ptr<types::Type>>> params;
|
||||
bool is_vararg = false;
|
||||
while (inner.peek().content != ")") {
|
||||
if (params.size() > 0) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user