-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDecl.h
71 lines (59 loc) · 1.68 KB
/
Decl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/** @file Decl.h
* @brief Decl及其子类
**/
#pragma once
#include <string>
#include <memory>
#include <vector>
#include "Expr.h"
#include "ASTNode.h"
namespace AST
{
class FunctionDecl;
class Decl : public ASTNode
{
};
/**
* @brief 抽象语法树的根节点,无实际含义
*/
class TranslationUnitDecl : public Decl
{
std::vector<std::unique_ptr<Decl>> decls;
public:
TranslationUnitDecl() = default;
TranslationUnitDecl(std::vector<std::unique_ptr<Decl>> &rhsDecls) : decls(std::move(rhsDecls)) {}
virtual json toJson() const override;
// 通过 Decl 继承
virtual Value* genCode(CodeGeneratorBase& generator) const override;
};
/**
* @brief 变量定义
*/
class VarDecl : public Decl
{
friend CodeGenerator;
protected:
friend class FunctionDecl;
std::string name;
std::string type;
bool hasInit;
std::unique_ptr<Expr> initValue;
public:
VarDecl() = default;
VarDecl(const std::string &type, const std::string &name, bool hasInit = false,
std::unique_ptr<Expr> initValue = nullptr) : type(type), name(name), hasInit(hasInit), initValue(std::move(initValue)){};
const std::string getName() const
{
return name;
}
virtual json toJson() const override;
// 通过 Decl 继承
virtual Value* genCode(CodeGeneratorBase& generator) const override;
};
class ParmVarDecl : public VarDecl
{
public:
ParmVarDecl(const std::string &type, const std::string &name) : VarDecl(type, name) {}
virtual json toJson() const override;
};
}; // namespace AST