|
| 1 | +#include "RPCLibraryHeaderEmitter.h" |
| 2 | +#include "RPCCommon.h" |
| 3 | + |
| 4 | +#include "clang/AST/AST.h" |
| 5 | +#include "clang/AST/Mangle.h" |
| 6 | +#include "clang/Frontend/CompilerInstance.h" |
| 7 | + |
| 8 | +#include "llvm/ADT/StringRef.h" |
| 9 | +#include "llvm/Support/ToolOutputFile.h" |
| 10 | +#include "llvm/Support/raw_ostream.h" |
| 11 | + |
| 12 | +using namespace clang; |
| 13 | +using namespace lldb_rpc_gen; |
| 14 | + |
| 15 | +void RPCLibraryHeaderEmitter::StartClass(std::string ClassName) { |
| 16 | + CurrentClass = std::move(ClassName); |
| 17 | + std::string BaseClass = |
| 18 | + lldb_rpc_gen::SBClassInheritsFromObjectRef(CurrentClass) |
| 19 | + ? "ObjectRef" |
| 20 | + : "LocalObjectRef"; |
| 21 | + EmitLine("class " + CurrentClass + " : public rpc::" + BaseClass + " {"); |
| 22 | + EmitLine("public:"); |
| 23 | + IndentLevel++; |
| 24 | + if (lldb_rpc_gen::SBClassRequiresDefaultCtor(CurrentClass)) |
| 25 | + EmitLine(CurrentClass + "();"); |
| 26 | + |
| 27 | + // NOTE: There's currently only one RPC-specific extension that is actually |
| 28 | + // used AFAICT. We can generalize this if we need more. |
| 29 | + if (CurrentClass == "SBDebugger") |
| 30 | + EmitLine("int SetIOFile(const char *path);"); |
| 31 | +} |
| 32 | + |
| 33 | +void RPCLibraryHeaderEmitter::EndClass() { |
| 34 | + if (lldb_rpc_gen::SBClassRequiresCopyCtorAssign(CurrentClass)) { |
| 35 | + if (!CopyCtorEmitted) |
| 36 | + EmitLine(CurrentClass + "(const lldb_rpc::" + CurrentClass + " &rhs);"); |
| 37 | + if (!CopyAssignEmitted) |
| 38 | + EmitLine(CurrentClass + " &operator=(const lldb_rpc::" + CurrentClass + |
| 39 | + " &rhs);"); |
| 40 | + } |
| 41 | + if (!MoveCtorEmitted) |
| 42 | + EmitLine(CurrentClass + "(lldb_rpc::" + CurrentClass + " &&rhs);"); |
| 43 | + if (!MoveAssignEmitted) |
| 44 | + EmitLine(CurrentClass + " &operator=(" + CurrentClass + " &&rhs);"); |
| 45 | + |
| 46 | + IndentLevel--; |
| 47 | + EmitLine("}; // class " + CurrentClass); |
| 48 | + CurrentClass.clear(); |
| 49 | +} |
| 50 | + |
| 51 | +void RPCLibraryHeaderEmitter::EmitMethod(const Method &method) { |
| 52 | + std::string DeclarationLine; |
| 53 | + llvm::raw_string_ostream DeclarationLineStream(DeclarationLine); |
| 54 | + |
| 55 | + if (method.IsExplicitCtorOrConversionMethod) |
| 56 | + DeclarationLineStream << "explicit "; |
| 57 | + else if (!method.IsInstance) |
| 58 | + DeclarationLineStream << "static "; |
| 59 | + |
| 60 | + if (!method.IsDtor && !method.IsConversionMethod && !method.IsCtor) { |
| 61 | + DeclarationLineStream << lldb_rpc_gen::ReplaceLLDBNamespaceWithRPCNamespace( |
| 62 | + method.ReturnType.getAsString(method.Policy)) |
| 63 | + << " "; |
| 64 | + } |
| 65 | + |
| 66 | + DeclarationLineStream << method.BaseName << "(" |
| 67 | + << method.CreateParamListAsString( |
| 68 | + eLibrary, /*IncludeDefaultValue = */ true) |
| 69 | + << ")"; |
| 70 | + if (method.IsConst) |
| 71 | + DeclarationLineStream << " const"; |
| 72 | + DeclarationLineStream << ";"; |
| 73 | + |
| 74 | + EmitLine(DeclarationLine); |
| 75 | + |
| 76 | + if (method.IsCopyCtor) |
| 77 | + CopyCtorEmitted = true; |
| 78 | + else if (method.IsCopyAssign) |
| 79 | + CopyAssignEmitted = true; |
| 80 | + else if (method.IsMoveCtor) |
| 81 | + MoveCtorEmitted = true; |
| 82 | + else if (method.IsMoveAssign) |
| 83 | + MoveAssignEmitted = true; |
| 84 | +} |
| 85 | + |
| 86 | +void RPCLibraryHeaderEmitter::EmitEnum(EnumDecl *E) { |
| 87 | + // NOTE: All of the enumerations embedded in SB classes are currently |
| 88 | + // anonymous and backed by an unsigned int. |
| 89 | + EmitLine("enum : unsigned {"); |
| 90 | + IndentLevel++; |
| 91 | + for (const EnumConstantDecl *EC : E->enumerators()) { |
| 92 | + std::string EnumValue = EC->getNameAsString(); |
| 93 | + SmallString<16> ValueStr; |
| 94 | + EC->getInitVal().toString(ValueStr); |
| 95 | + EnumValue += " = " + ValueStr.str().str() + ", "; |
| 96 | + EmitLine(EnumValue); |
| 97 | + } |
| 98 | + |
| 99 | + IndentLevel--; |
| 100 | + EmitLine("};"); |
| 101 | +} |
| 102 | + |
| 103 | +std::string RPCLibraryHeaderEmitter::GetHeaderGuard() { |
| 104 | + const std::string UpperFilenameNoExt = |
| 105 | + llvm::sys::path::stem( |
| 106 | + llvm::sys::path::filename(OutputFile->getFilename())) |
| 107 | + .upper(); |
| 108 | + return "GENERATED_LLDB_RPC_LIBRARY_" + UpperFilenameNoExt + "_H"; |
| 109 | +} |
| 110 | + |
| 111 | +void RPCLibraryHeaderEmitter::Begin() { |
| 112 | + const std::string HeaderGuard = GetHeaderGuard(); |
| 113 | + EmitLine("#ifndef " + HeaderGuard); |
| 114 | + EmitLine("#define " + HeaderGuard); |
| 115 | + EmitLine(""); |
| 116 | + EmitLine("#include <lldb-rpc/common/RPCPublic.h>"); |
| 117 | + EmitLine("#include \"SBDefines.h\""); |
| 118 | + EmitLine("#include \"LLDBRPC.h\""); |
| 119 | + EmitLine(""); |
| 120 | + EmitLine("namespace lldb_rpc {"); |
| 121 | +} |
| 122 | + |
| 123 | +void RPCLibraryHeaderEmitter::End() { |
| 124 | + EmitLine("} // namespace lldb_rpc"); |
| 125 | + EmitLine("#endif // " + GetHeaderGuard()); |
| 126 | +} |
0 commit comments