slightly optimize and refactor

This commit is contained in:
2024-10-23 09:25:13 +00:00
parent c6e529ec0f
commit 77719e07e7
7 changed files with 11 additions and 30 deletions

View File

@ -8,7 +8,7 @@
#include "ast/scope.hpp"
#include "tools.h"
class IRBuilder : public ASTNodeVirturalVisitor {
friend std::shared_ptr<ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
public:
std::shared_ptr<ModuleItem> prog;
std::shared_ptr<TypeDefItem> cur_class;
std::shared_ptr<FunctionDefItem> cur_func;

View File

@ -20,7 +20,7 @@ class LLVMIRItemBase {
};
class TypeDefItem : public LLVMIRItemBase {
friend class IRBuilder;
public:
std::string class_name_raw;
std::vector<LLVMType> elements;
@ -411,8 +411,7 @@ class FunctionDefItem : public LLVMIRItemBase {
}
};
class FunctionDeclareItem : public LLVMIRItemBase {
friend class IRBuilder;
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
public:
LLVMType return_type;
std::string func_name_raw;
std::vector<LLVMType> args;

View File

@ -8,11 +8,7 @@
#include "tools.h"
class ASTNodeBase {
friend Visitor;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
protected:
public:
std::shared_ptr<ScopeBase> current_scope;
ASTNodeType type;
// std::vector<std::shared_ptr<ASTNodeBase>> children;

View File

@ -88,10 +88,7 @@ struct FunctionSchema {
std::vector<std::pair<ExprTypeInfo, std::string>> arguments;
};
class FunctionScope : public ScopeBase {
friend std::shared_ptr<class Program_ASTNode> CheckAndDecorate(std::shared_ptr<class Program_ASTNode> src);
friend class Visitor;
friend class ASTSemanticCheckVisitor;
friend class GlobalScope;
public:
FunctionSchema schema;
bool add_variable([[maybe_unused]] const std::string &name, [[maybe_unused]] const ExprTypeInfo &type) override {
throw std::runtime_error("FunctionScope does not support add_variable");
@ -132,9 +129,7 @@ class FunctionScope : public ScopeBase {
}
};
class ClassDefScope : public ScopeBase {
friend class Visitor;
friend class GlobalScope;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
public:
std::unordered_map<std::string, ExprTypeInfo> member_variables;
std::unordered_map<std::string, std::shared_ptr<FunctionScope>> member_functions;
IRClassInfo llvm_class_info;

View File

@ -6,6 +6,7 @@
#include "structural_astnode.h"
class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
public:
bool is_in_func_def;
bool has_return;
FunctionSchema cur_func_schema;
@ -13,7 +14,6 @@ class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
bool is_in_class_def;
size_t loop_level;
std::shared_ptr<GlobalScope> global_scope;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
bool ClassExists(const std::string &name) {
if (name == "int" || name == "bool") return true;

View File

@ -8,9 +8,7 @@
#include "expr_astnode.h"
#include "statement_astnode.h"
class FuncDef_ASTNode : public ASTNodeBase {
friend Visitor;
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
public:
bool is_constructor;
IdentifierType func_name;
ExprTypeInfo return_type;
@ -22,11 +20,7 @@ class FuncDef_ASTNode : public ASTNodeBase {
virtual void accept(class ASTNodeVisitorBase *visitor) override;
};
class ClassDef_ASTNode : public ASTNodeBase {
friend Visitor;
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
private:
public:
std::string class_name;
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> member_variables;
std::vector<std::shared_ptr<FuncDef_ASTNode>> member_functions;
@ -38,16 +32,12 @@ class ClassDef_ASTNode : public ASTNodeBase {
virtual void accept(class ASTNodeVisitorBase *visitor) override;
};
class Program_ASTNode : public ASTNodeBase {
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
friend Visitor;
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
public:
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> global_variables;
std::vector<std::shared_ptr<ClassDef_ASTNode>> classes;
std::vector<std::shared_ptr<FuncDef_ASTNode>> functions;
std::vector<std::shared_ptr<ASTNodeBase>> sorted_children;
public:
Program_ASTNode() = default;
virtual void accept(class ASTNodeVisitorBase *visitor) override;
};