video_core/ast: Supply const accessors for data where applicable

Provides const equivalents of data accessors for use within const
contexts.
This commit is contained in:
Lioncash 2019-10-05 08:06:44 -04:00
parent 3728bbc22a
commit 8e0c80f269
2 changed files with 41 additions and 37 deletions

View File

@ -185,9 +185,7 @@ void ASTZipper::Remove(const ASTNode node) {
class ExprPrinter final { class ExprPrinter final {
public: public:
ExprPrinter() = default; void operator()(const ExprAnd& expr) {
void operator()(ExprAnd const& expr) {
inner += "( "; inner += "( ";
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
inner += " && "; inner += " && ";
@ -195,7 +193,7 @@ public:
inner += ')'; inner += ')';
} }
void operator()(ExprOr const& expr) { void operator()(const ExprOr& expr) {
inner += "( "; inner += "( ";
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
inner += " || "; inner += " || ";
@ -203,29 +201,29 @@ public:
inner += ')'; inner += ')';
} }
void operator()(ExprNot const& expr) { void operator()(const ExprNot& expr) {
inner += "!"; inner += "!";
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
} }
void operator()(ExprPredicate const& expr) { void operator()(const ExprPredicate& expr) {
inner += "P" + std::to_string(expr.predicate); inner += "P" + std::to_string(expr.predicate);
} }
void operator()(ExprCondCode const& expr) { void operator()(const ExprCondCode& expr) {
u32 cc = static_cast<u32>(expr.cc); u32 cc = static_cast<u32>(expr.cc);
inner += "CC" + std::to_string(cc); inner += "CC" + std::to_string(cc);
} }
void operator()(ExprVar const& expr) { void operator()(const ExprVar& expr) {
inner += "V" + std::to_string(expr.var_index); inner += "V" + std::to_string(expr.var_index);
} }
void operator()(ExprBoolean const& expr) { void operator()(const ExprBoolean& expr) {
inner += expr.value ? "true" : "false"; inner += expr.value ? "true" : "false";
} }
std::string& GetResult() { const std::string& GetResult() const {
return inner; return inner;
} }
@ -234,9 +232,7 @@ public:
class ASTPrinter { class ASTPrinter {
public: public:
ASTPrinter() = default; void operator()(const ASTProgram& ast) {
void operator()(ASTProgram& ast) {
scope++; scope++;
inner += "program {\n"; inner += "program {\n";
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
@ -248,7 +244,7 @@ public:
scope--; scope--;
} }
void operator()(ASTIfThen& ast) { void operator()(const ASTIfThen& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "if (" + expr_parser.GetResult() + ") {\n"; inner += Ident() + "if (" + expr_parser.GetResult() + ") {\n";
@ -262,7 +258,7 @@ public:
inner += Ident() + "}\n"; inner += Ident() + "}\n";
} }
void operator()(ASTIfElse& ast) { void operator()(const ASTIfElse& ast) {
inner += Ident() + "else {\n"; inner += Ident() + "else {\n";
scope++; scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
@ -274,34 +270,34 @@ public:
inner += Ident() + "}\n"; inner += Ident() + "}\n";
} }
void operator()(ASTBlockEncoded& ast) { void operator()(const ASTBlockEncoded& ast) {
inner += Ident() + "Block(" + std::to_string(ast.start) + ", " + std::to_string(ast.end) + inner += Ident() + "Block(" + std::to_string(ast.start) + ", " + std::to_string(ast.end) +
");\n"; ");\n";
} }
void operator()(ASTBlockDecoded& ast) { void operator()(const ASTBlockDecoded& ast) {
inner += Ident() + "Block;\n"; inner += Ident() + "Block;\n";
} }
void operator()(ASTVarSet& ast) { void operator()(const ASTVarSet& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += inner +=
Ident() + "V" + std::to_string(ast.index) + " := " + expr_parser.GetResult() + ";\n"; Ident() + "V" + std::to_string(ast.index) + " := " + expr_parser.GetResult() + ";\n";
} }
void operator()(ASTLabel& ast) { void operator()(const ASTLabel& ast) {
inner += "Label_" + std::to_string(ast.index) + ":\n"; inner += "Label_" + std::to_string(ast.index) + ":\n";
} }
void operator()(ASTGoto& ast) { void operator()(const ASTGoto& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> goto Label_" + inner += Ident() + "(" + expr_parser.GetResult() + ") -> goto Label_" +
std::to_string(ast.label) + ";\n"; std::to_string(ast.label) + ";\n";
} }
void operator()(ASTDoWhile& ast) { void operator()(const ASTDoWhile& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "do {\n"; inner += Ident() + "do {\n";
@ -315,14 +311,14 @@ public:
inner += Ident() + "} while (" + expr_parser.GetResult() + ");\n"; inner += Ident() + "} while (" + expr_parser.GetResult() + ");\n";
} }
void operator()(ASTReturn& ast) { void operator()(const ASTReturn& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> " + inner += Ident() + "(" + expr_parser.GetResult() + ") -> " +
(ast.kills ? "discard" : "exit") + ";\n"; (ast.kills ? "discard" : "exit") + ";\n";
} }
void operator()(ASTBreak& ast) { void operator()(const ASTBreak& ast) {
ExprPrinter expr_parser{}; ExprPrinter expr_parser{};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
inner += Ident() + "(" + expr_parser.GetResult() + ") -> break;\n"; inner += Ident() + "(" + expr_parser.GetResult() + ") -> break;\n";
@ -341,7 +337,7 @@ public:
std::visit(*this, *node->GetInnerData()); std::visit(*this, *node->GetInnerData());
} }
std::string& GetResult() { const std::string& GetResult() const {
return inner; return inner;
} }
@ -696,7 +692,7 @@ class ASTClearer {
public: public:
ASTClearer() = default; ASTClearer() = default;
void operator()(ASTProgram& ast) { void operator()(const ASTProgram& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
Visit(current); Visit(current);
@ -704,7 +700,7 @@ public:
} }
} }
void operator()(ASTIfThen& ast) { void operator()(const ASTIfThen& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
Visit(current); Visit(current);
@ -712,7 +708,7 @@ public:
} }
} }
void operator()(ASTIfElse& ast) { void operator()(const ASTIfElse& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
Visit(current); Visit(current);
@ -720,19 +716,19 @@ public:
} }
} }
void operator()(ASTBlockEncoded& ast) {} void operator()([[maybe_unused]] const ASTBlockEncoded& ast) {}
void operator()(ASTBlockDecoded& ast) { void operator()(ASTBlockDecoded& ast) {
ast.nodes.clear(); ast.nodes.clear();
} }
void operator()(ASTVarSet& ast) {} void operator()([[maybe_unused]] const ASTVarSet& ast) {}
void operator()(ASTLabel& ast) {} void operator()([[maybe_unused]] const ASTLabel& ast) {}
void operator()(ASTGoto& ast) {} void operator()([[maybe_unused]] const ASTGoto& ast) {}
void operator()(ASTDoWhile& ast) { void operator()(const ASTDoWhile& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
Visit(current); Visit(current);
@ -740,11 +736,11 @@ public:
} }
} }
void operator()(ASTReturn& ast) {} void operator()([[maybe_unused]] const ASTReturn& ast) {}
void operator()(ASTBreak& ast) {} void operator()([[maybe_unused]] const ASTBreak& ast) {}
void Visit(ASTNode& node) { void Visit(const ASTNode& node) {
std::visit(*this, *node->GetInnerData()); std::visit(*this, *node->GetInnerData());
node->Clear(); node->Clear();
} }

View File

@ -48,11 +48,11 @@ public:
void Init(ASTNode first, ASTNode parent); void Init(ASTNode first, ASTNode parent);
ASTNode GetFirst() { ASTNode GetFirst() const {
return first; return first;
} }
ASTNode GetLast() { ASTNode GetLast() const {
return last; return last;
} }
@ -177,6 +177,10 @@ public:
return &data; return &data;
} }
const ASTData* GetInnerData() const {
return &data;
}
ASTNode GetNext() const { ASTNode GetNext() const {
return next; return next;
} }
@ -189,6 +193,10 @@ public:
return *manager; return *manager;
} }
const ASTZipper& GetManager() const {
return *manager;
}
std::optional<u32> GetGotoLabel() const { std::optional<u32> GetGotoLabel() const {
auto inner = std::get_if<ASTGoto>(&data); auto inner = std::get_if<ASTGoto>(&data);
if (inner) { if (inner) {