Skip to content

Commit

Permalink
✨ feat(Gen): 结构体可以被作为自定义类型了
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Aug 7, 2024
1 parent 9ac0589 commit 7652f47
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 13 deletions.
13 changes: 13 additions & 0 deletions docs/Class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Class

## 成员 / 字段

成员和字段的初始值应该在构造函数中完成,默认以成员/字段的显式定义中的值为准,否则以构造函数中的复制函数为准

成员/字段的名称为原本名称

成员/字段在内存中的存储顺序为定义的顺序

## 函数

函数不为
15 changes: 15 additions & 0 deletions src/Tools/Managers/ClassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@
#include "ClassManager.h"

namespace Riddle {
/// @brief 查找相关的类
/// <p>
/// a
/// </p>
Class ClassManager::getClass(const std::string &name) {
const auto it = Classes.find(name);
if (it == Classes.end()) {
throw std::logic_error("没有这个类或者使用了不支持的路径");
}
return Classes.find(name)->second;
}

void ClassManager::createClass(Class theClass) {
Classes[theClass.types->getName().str()] = theClass;
}
}// namespace Riddle
10 changes: 8 additions & 2 deletions src/Tools/Managers/ClassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
#ifndef RIDDLE_LANGUAGE_CLASSMANAGER_H
#define RIDDLE_LANGUAGE_CLASSMANAGER_H
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Value.h>

#include "Types/Class.h"

namespace Riddle {
/// @brief 管理类
class ClassManager {
/// @brief 存储数据字段
llvm::StructType *data;
std::unordered_map<std::string, Class> Classes;
public:
Class getClass(const std::string &name);

void createClass(Class theClass);
};

}// namespace Riddle
Expand Down
9 changes: 9 additions & 0 deletions src/Types/Class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Created by wangz on 24-8-7.
//

#include "Class.h"

namespace Riddle{

} // Riddle
19 changes: 19 additions & 0 deletions src/Types/Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by wangz on 24-8-7.
//

#ifndef CLASS_H
#define CLASS_H

#include <llvm/IR/DerivedTypes.h>

namespace Riddle{

class Class {
public:
llvm::StructType *types = nullptr;
};

} // Riddle

#endif //CLASS_H
13 changes: 9 additions & 4 deletions src/Visitors/GenVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,14 @@ namespace Riddle{
}

std::any GenVisitor::visitTypeName(RiddleParser::TypeNameContext *ctx){
auto name = ctx->name->getText();
// 内置类型
const auto name = ctx->name->getText();
if (isSampleType(name)) {
return getSampleType(name, Builder);
}
throw std::logic_error("关于使用自定义类型是未实现的");
// 自定义类型
auto type = classManager.getClass(name);
return llvm::dyn_cast<llvm::Type>(type.types);
}

std::any GenVisitor::visitPtrExpr(RiddleParser::PtrExprContext *ctx){
Expand All @@ -516,14 +519,16 @@ namespace Riddle{

std::any GenVisitor::visitClassDefine(RiddleParser::ClassDefineContext *ctx){
const std::string name = packStack.top() + ctx->className->getText();
llvm::StructType *structTy = llvm::StructType::create(globalContext, name);
Class theClass;
theClass.types = llvm::StructType::create(globalContext, name);
packStack.push(packStack.top() + ctx->className->getText());
varManager.push();
ParentStack.push(structTy);
ParentStack.push(theClass.types);
visit(ctx->body);
packStack.pop();
varManager.pop();
ParentStack.pop();
classManager.createClass(theClass);
return nullptr;
}

Expand Down
19 changes: 12 additions & 7 deletions src/Visitors/GenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@
#include <variant>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>

#include "Tools/Managers/ClassManager.h"

namespace Riddle {
/// @brief 用于实现生成 IR 的类
class GenVisitor : public RiddleParserBaseVisitor {
private:
class GenVisitor final :public RiddleParserBaseVisitor {
castMapTy cast;
/// @brief 用于函数或对象的唯一性
std::stack<std::string> packStack;
std::unordered_map<std::string, llvm::FunctionCallee> FuncCalls;
std::stack<std::variant<llvm::Function *, llvm::StructType *> > ParentStack;
VarManager varManager;
llvm::IRBuilder<> Builder;
llvm::LLVMContext globalContext;
llvm::Module *module;
// stack
std::stack<std::variant<llvm::Function *, llvm::StructType *> > ParentStack;
std::stack<std::string> packStack;
// manager
VarManager varManager;
ClassManager classManager;
// region 工具
public:
/// @brief 用于处理二元操作
Expand All @@ -37,9 +42,9 @@ namespace Riddle {
llvm::Value *assignBinaryOp(llvm::Value *var, llvm::Value *value, const std::string &op);

// endregion
public:

[[maybe_unused]]
GenVisitor(const std::string &builder);
explicit GenVisitor(const std::string &builder);

/// @brief 程序的根节点
/// @returns null
Expand Down

0 comments on commit 7652f47

Please sign in to comment.