aboutsummaryrefslogtreecommitdiff
path: root/lldb/tools/lldb-rpc-gen/RPCCommon.h
blob: 2a309d05e5ef748eeeff7c048a435436a7c224a7 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===-- RPCCommon.h -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_RPC_GEN_RPCCOMMON_H
#define LLDB_RPC_GEN_RPCCOMMON_H

#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"

#include <string>

using namespace clang;

namespace lldb_rpc_gen {
QualType GetUnderlyingType(QualType T);
QualType GetUnqualifiedUnderlyingType(QualType T);
std::string GetMangledName(ASTContext &Context, CXXMethodDecl *MDecl);

bool TypeIsFromLLDBPrivate(QualType T);
bool TypeIsSBClass(QualType T);
bool TypeIsConstCharPtr(QualType T);
bool TypeIsConstCharPtrPtr(QualType T);
bool TypeIsDisallowedClass(QualType T);
bool TypeIsCallbackFunctionPointer(QualType T);

bool MethodIsDisallowed(ASTContext &Context, CXXMethodDecl *MDecl);

std::string ReplaceLLDBNamespaceWithRPCNamespace(std::string Name);
std::string StripLLDBNamespace(std::string Name);
bool SBClassRequiresDefaultCtor(const std::string &ClassName);
bool SBClassRequiresCopyCtorAssign(const std::string &ClassName);
bool SBClassInheritsFromObjectRef(const std::string &ClassName);
std::string GetSBClassNameFromType(QualType T);
struct Param {
  std::string Name;
  QualType Type;
  std::string DefaultValueText;
  bool IsFollowedByLen;
};

enum GenerationKind : bool { eServer, eLibrary };

struct Method {
  enum Type { eOther, eConstructor, eDestructor };

  Method(CXXMethodDecl *MDecl, const PrintingPolicy &Policy,
         ASTContext &Context);

  // Adding a '<' allows us to use Methods in ordered containers.
  // The ordering is on memory addresses.
  bool operator<(const lldb_rpc_gen::Method &rhs) const;
  const PrintingPolicy &Policy;
  const ASTContext &Context;
  std::string QualifiedName;
  std::string BaseName;
  std::string MangledName;
  QualType ReturnType;
  QualType ThisType;
  std::vector<Param> Params;
  bool IsConst = false;
  bool IsInstance = false;
  bool IsCtor = false;
  bool IsCopyCtor = false;
  bool IsCopyAssign = false;
  bool IsMoveCtor = false;
  bool IsMoveAssign = false;
  bool IsDtor = false;
  bool IsConversionMethod = false;
  bool IsExplicitCtorOrConversionMethod = false;
  bool ContainsFunctionPointerParameter = false;

  std::string CreateParamListAsString(GenerationKind Generation,
                                      bool IncludeDefaultValue = false) const;

  bool RequiresConnectionParameter() const;
};

std::string
GetDefaultArgumentsForConstructor(std::string ClassName,
                                  const lldb_rpc_gen::Method &method);

class FileEmitter {
protected:
  FileEmitter(std::unique_ptr<llvm::ToolOutputFile> &&OutputFile)
      : OutputFile(std::move(OutputFile)), IndentLevel(0) {}
  void EmitLine(const std::string &line) {
    for (auto i = 0; i < IndentLevel; i++)
      OutputFile->os() << "  ";

    OutputFile->os() << line << "\n";
  }

  void EmitNewLine() { OutputFile->os() << "\n"; }

  std::unique_ptr<llvm::ToolOutputFile> OutputFile;
  uint8_t IndentLevel;
};
} // namespace lldb_rpc_gen
#endif // LLDB_RPC_GEN_RPCCOMMON_H