Skip to content

Commit cec93b3

Browse files
committed
[lldb][rpc] Add RPC client library emitters
Adds the RPC client library sources and header emitters.
1 parent 431333f commit cec93b3

File tree

4 files changed

+802
-0
lines changed

4 files changed

+802
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef LLDB_RPC_GEN_RPCLIBRARYHEADEREMITTER_H
2+
#define LLDB_RPC_GEN_RPCLIBRARYHEADEREMITTER_H
3+
4+
#include "RPCCommon.h"
5+
6+
#include "clang/AST/AST.h"
7+
#include "llvm/Support/ToolOutputFile.h"
8+
9+
using namespace clang;
10+
11+
namespace lldb_rpc_gen {
12+
13+
class RPCLibraryHeaderEmitter : public FileEmitter {
14+
public:
15+
RPCLibraryHeaderEmitter(std::unique_ptr<llvm::ToolOutputFile> &&OutputFile)
16+
: FileEmitter(std::move(OutputFile)), CurrentClass() {
17+
Begin();
18+
}
19+
20+
~RPCLibraryHeaderEmitter() { End(); }
21+
22+
void StartClass(std::string ClassName);
23+
24+
void EndClass();
25+
26+
void EmitMethod(const Method &method);
27+
28+
void EmitEnum(EnumDecl *E);
29+
30+
private:
31+
std::string GetHeaderGuard();
32+
33+
void Begin();
34+
35+
void End();
36+
37+
std::string CurrentClass;
38+
bool CopyCtorEmitted = false;
39+
bool CopyAssignEmitted = false;
40+
bool MoveCtorEmitted = false;
41+
bool MoveAssignEmitted = false;
42+
};
43+
} // namespace lldb_rpc_gen
44+
#endif // LLDB_RPC_GEN_RPCLIBRARYHEADEREMITTER_H

0 commit comments

Comments
 (0)