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