|
| 1 | +#include "AST/Exprs.hpp" |
| 2 | +#include "AST/Types.hpp" |
| 3 | +#include "ASTVisitor.hpp" |
| 4 | +#include "TypePrinter.hpp" |
| 5 | + |
| 6 | +#include <llvm/Support/raw_ostream.h> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace glu::ast { |
| 10 | + |
| 11 | +/// @brief CodePrinter is a visitor that converts AST nodes back to Glu source |
| 12 | +/// code. |
| 13 | +/// |
| 14 | +/// This class generates valid Glu source code from AST nodes, primarily for |
| 15 | +/// decompilation of function prototypes and type declarations. It supports: |
| 16 | +/// - FunctionDecl (without body) |
| 17 | +/// - StructDecl |
| 18 | +/// - EnumDecl |
| 19 | +/// |
| 20 | +/// The generated code should be syntactically valid Glu code that can be |
| 21 | +/// used as interface declarations. |
| 22 | +class CodePrinter : public ASTVisitor<CodePrinter> { |
| 23 | + llvm::raw_ostream &_out; ///< The output stream to write the code to |
| 24 | + TypePrinter _typePrinter; ///< Type printer for formatting types |
| 25 | + size_t _indent = 0; ///< Current indentation level |
| 26 | + |
| 27 | +public: |
| 28 | + /// @brief Constructs a CodePrinter object. |
| 29 | + /// @param out The output stream to write the generated code to. |
| 30 | + CodePrinter(llvm::raw_ostream &out = llvm::outs()) |
| 31 | + : _out(out), _typePrinter(true /* enable type variable names */) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + /// @brief Visit a ModuleDecl and print its contents as Glu code |
| 36 | + /// @param node The ModuleDecl node to print |
| 37 | + void visitModuleDecl(ModuleDecl *node) |
| 38 | + { |
| 39 | + // Print each top-level declaration in the module |
| 40 | + for (auto *decl : node->getDecls()) { |
| 41 | + visit(decl); |
| 42 | + _out << "\n"; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// @brief Visit a FunctionDecl and print its signature (without body) |
| 47 | + /// @param node The FunctionDecl node to print |
| 48 | + void visitFunctionDecl(FunctionDecl *node) |
| 49 | + { |
| 50 | + printDeclPrefix(node); |
| 51 | + |
| 52 | + _out << "func " << node->getName(); |
| 53 | + |
| 54 | + printFunctionParameters(node->getParams()); |
| 55 | + |
| 56 | + if (auto *funcType = node->getType()) { |
| 57 | + auto *returnType = funcType->getReturnType(); |
| 58 | + if (!llvm::isa<glu::types::VoidTy>(returnType)) { |
| 59 | + _out << " -> "; |
| 60 | + printType(returnType); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + _out << ";"; |
| 65 | + } |
| 66 | + |
| 67 | + /// @brief Visit a StructDecl and print its definition |
| 68 | + /// @param node The StructDecl node to print |
| 69 | + void visitStructDecl(StructDecl *node) |
| 70 | + { |
| 71 | + printDeclPrefix(node); |
| 72 | + |
| 73 | + _out << "struct " << node->getName() << " {\n"; |
| 74 | + |
| 75 | + _indent += 4; |
| 76 | + |
| 77 | + for (auto *field : node->getFields()) { |
| 78 | + visit(field); |
| 79 | + _out << "\n"; |
| 80 | + } |
| 81 | + |
| 82 | + _indent -= 4; |
| 83 | + |
| 84 | + printIndent(); |
| 85 | + |
| 86 | + _out << "}"; |
| 87 | + } |
| 88 | + |
| 89 | + /// @brief Visit an EnumDecl and print its definition |
| 90 | + /// @param node The EnumDecl node to print |
| 91 | + void visitEnumDecl(EnumDecl *node) |
| 92 | + { |
| 93 | + printDeclPrefix(node); |
| 94 | + |
| 95 | + _out << "enum " << node->getName() << " {\n"; |
| 96 | + |
| 97 | + _indent += 4; |
| 98 | + |
| 99 | + for (auto *field : node->getFields()) { |
| 100 | + visit(field); |
| 101 | + _out << "\n"; |
| 102 | + } |
| 103 | + |
| 104 | + _indent -= 4; |
| 105 | + |
| 106 | + printIndent(); |
| 107 | + |
| 108 | + _out << "}"; |
| 109 | + } |
| 110 | + |
| 111 | + /// @brief Visit a FieldDecl and print its declaration |
| 112 | + /// @param node The FieldDecl node to print |
| 113 | + void visitFieldDecl(FieldDecl *node) |
| 114 | + { |
| 115 | + printDeclPrefix(node); |
| 116 | + |
| 117 | + _out << node->getName(); |
| 118 | + |
| 119 | + // For enum fields, we don't print the type (just the name) |
| 120 | + // For struct fields, we print "name: type" |
| 121 | + if (!llvm::isa_and_nonnull<EnumDecl>(node->getParent())) { |
| 122 | + _out << ": "; |
| 123 | + printType(node->getType()); |
| 124 | + } |
| 125 | + |
| 126 | + _out << ","; |
| 127 | + } |
| 128 | + |
| 129 | + /// @brief Visit a ParamDecl and print its declaration |
| 130 | + /// @param node The ParamDecl node to print |
| 131 | + void visitParamDecl(ParamDecl *node) |
| 132 | + { |
| 133 | + visit(node->getAttributes()); |
| 134 | + _out << node->getName() << ": "; |
| 135 | + printType(node->getType()); |
| 136 | + } |
| 137 | + |
| 138 | + /// @brief Visit a LiteralExpr and print its value for attribute parameters |
| 139 | + /// @param node The LiteralExpr node to print |
| 140 | + void visitLiteralExpr(LiteralExpr *node) |
| 141 | + { |
| 142 | + std::visit( |
| 143 | + [this](auto &&val) { |
| 144 | + using T = std::decay_t<decltype(val)>; |
| 145 | + if constexpr (std::is_same_v<T, llvm::APInt>) { |
| 146 | + _out << val; |
| 147 | + } else if constexpr (std::is_same_v<T, llvm::APFloat>) { |
| 148 | + _out << val.convertToDouble(); |
| 149 | + } else if constexpr (std::is_same_v<T, llvm::StringRef>) { |
| 150 | + _out << "\"" << val.str() << "\""; |
| 151 | + } else if constexpr (std::is_same_v<T, bool>) { |
| 152 | + _out << (val ? "true" : "false"); |
| 153 | + } else if constexpr (std::is_same_v<T, std::nullptr_t>) { |
| 154 | + _out << "null"; |
| 155 | + } |
| 156 | + }, |
| 157 | + node->getValue() |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + void visitAttribute(Attribute *node) |
| 162 | + { |
| 163 | + _out << "@" << node->getAttributeKindSpelling(); |
| 164 | + |
| 165 | + if (node->getParameter()) { |
| 166 | + _out << "("; |
| 167 | + visit(node->getParameter()); |
| 168 | + _out << ")"; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + void visitAttributeList(AttributeList *node) |
| 173 | + { |
| 174 | + for (auto *attr : node->getAttributes()) { |
| 175 | + visit(attr); |
| 176 | + _out << " "; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | +private: |
| 181 | + /// @brief Print indentation |
| 182 | + void printIndent() { _out.indent(_indent); } |
| 183 | + |
| 184 | + /// @brief Print a type using the enhanced type printer |
| 185 | + /// @param type The type to print |
| 186 | + void printType(glu::types::TypeBase *type) |
| 187 | + { |
| 188 | + if (type) { |
| 189 | + _out << _typePrinter.visit(type); |
| 190 | + } else { |
| 191 | + _out << "void"; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /// @brief Print attributes and visibility prefix for declarations |
| 196 | + /// @param decl The declaration to print prefix for |
| 197 | + void printDeclPrefix(DeclBase *decl) |
| 198 | + { |
| 199 | + printIndent(); |
| 200 | + visit(decl->getAttributes()); |
| 201 | + printVisibility(decl->getVisibility()); |
| 202 | + } |
| 203 | + |
| 204 | + /// @brief Print function parameters |
| 205 | + /// @param params The parameter list |
| 206 | + void printFunctionParameters(llvm::ArrayRef<ParamDecl *> params) |
| 207 | + { |
| 208 | + _out << "("; |
| 209 | + for (size_t i = 0; i < params.size(); ++i) { |
| 210 | + if (i > 0) { |
| 211 | + _out << ", "; |
| 212 | + } |
| 213 | + visit(params[i]); |
| 214 | + } |
| 215 | + _out << ")"; |
| 216 | + } |
| 217 | + |
| 218 | + /// @brief Print visibility modifier if present |
| 219 | + /// @param visibility The visibility to print |
| 220 | + void printVisibility(Visibility visibility) |
| 221 | + { |
| 222 | + switch (visibility) { |
| 223 | + case Visibility::Public: _out << "public "; break; |
| 224 | + case Visibility::Private: _out << "private "; break; |
| 225 | + } |
| 226 | + } |
| 227 | +}; |
| 228 | + |
| 229 | +void ASTNode::printInterface(llvm::raw_ostream &out) |
| 230 | +{ |
| 231 | + CodePrinter(out).visit(this); |
| 232 | +} |
| 233 | + |
| 234 | +} // namespace glu::ast |
0 commit comments