MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
annotation.cxx
Go to the documentation of this file.
1#include "pch.h"
3
4namespace {
5 template <typename T>
6 void ApplyAnnotation(clang::NamedDecl *decl, const std::string &key, const T &values) {
7 const auto &ctx = decl->getASTContext();
8
9 std::vector<clang::Expr *> args;
10 args.reserve(values.size());
11 for (size_t i = 0; i < values.size(); ++i) {
12 const auto arrayTy =
13 ctx.getConstantArrayType(
14 ctx.CharTy,
15 llvm::APInt(ctx.getTypeSize(ctx.getSizeType()), values[i].size() + 1),
16 nullptr,
17 clang::ArraySizeModifier::Normal,
18 0)
19 .withConst();
20
21 const auto literal =
22 clang::StringLiteral::Create(
23 decl->getASTContext(),
24 values[i],
25 clang::StringLiteralKind::Ordinary,
26 false,
27 arrayTy,
28 {});
29
30 args.push_back(clang::ConstantExpr::Create(decl->getASTContext(), literal));
31 }
32
33 // Same size of args makes args copied... Why???
34 decl->addAttr(clang::AnnotateAttr::CreateImplicit(
35 decl->getASTContext(),
36 mllif::shared::Namespace + '.' + key,
37 args.data(),
38 args.size()));
39 }
40
41 auto GetPath(const clang::NamedDecl *decl) -> std::deque<std::string> {
42 std::deque<std::string> namespaces;
43 for (; decl; decl = dyn_cast<clang::NamedDecl>(decl->getDeclContext())) {
44 if (auto name = decl->getName(); !name.empty()) {
45 namespaces.push_front(name.str());
46 }
47 }
48
49 return namespaces;
50 }
51} // namespace
52
53void mllif::shared::CreateAnnotation(clang::FunctionDecl *decl) {
54 const auto isMethod = clang::dyn_cast<clang::CXXMethodDecl>(decl);
55 const std::vector type = {(isMethod ? type::Method : type::Function)};
56 ApplyAnnotation(decl, prefix::Type, type);
57
58 const auto dirs = GetPath(decl);
59 ApplyAnnotation(decl, prefix::Path, dirs);
60}
constexpr std::string Path
Definition annotation.h:13
constexpr std::string Type
Definition annotation.h:14
constexpr std::string Method
Definition annotation.h:19
constexpr std::string Function
Definition annotation.h:18
void CreateAnnotation(clang::FunctionDecl *decl)
Annotate a declaration with its information that may be lost.
constexpr std::string Namespace
Definition annotation.h:10