MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
annotation.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pch.h"
18
20#include <llvm/Support/CommandLine.h>
21
22namespace {
23
24 constexpr char Delimiter = '\\';
25
26 /*
27 clang::ConstantExpr *CreateExpr(const clang::ASTContext& ctx, const std::string &str) {
28 const auto arrayTy =
29 ctx.getConstantArrayType(
30 ctx.CharTy,
31 llvm::APInt(ctx.getTypeSize(ctx.getSizeType()), str.size() + 1),
32 nullptr,
33 clang::ArraySizeModifier::Normal,
34 0)
35 .withConst();
36
37 const auto literal = clang::StringLiteral::Create(
38 ctx,
39 str,
40 clang::StringLiteralKind::Ordinary,
41 false,
42 arrayTy,
43 {});
44
45 return clang::ConstantExpr::Create(ctx, literal);
46 }
47 */
48
49 void ApplyAnnotation(clang::NamedDecl *decl, const std::string &key, const std::string* values, const size_t valuesSize) {
50 auto &ctx = decl->getASTContext();
51
52 /*
53 std::vector<clang::Expr *> args;
54 args.reserve(valuesSize);
55 for (auto i = 0; i < valuesSize; i++) {
56 args.push_back(CreateExpr(ctx, values[i]));
57 }
58
59 // Same size of args makes args copied... Why???
60 const auto attr = clang::AnnotateAttr::CreateImplicit(ctx, mllif::shared::Namespace + '.' + key, args.data(), args.size());
61 */
62
63 std::stringstream ss;
64 ss << mllif::shared::Namespace + '.' + key;
65 for (auto i = 0; i < valuesSize; ++i) {
66 ss << Delimiter << values[i];
67 }
68
69 const auto attr = clang::AnnotateAttr::CreateImplicit(ctx, ss.str(), nullptr, 0);
70
71 decl->addAttr(attr);
72 }
73
74 auto GetPath(const clang::NamedDecl *decl) -> std::vector<std::string> {
75 std::vector<std::string> namespaces;
76 for (; decl; decl = dyn_cast<clang::NamedDecl>(decl->getDeclContext())) {
77 if (auto name = decl->getDeclName().getAsString(); !name.empty()) {
78 namespaces.insert(namespaces.begin(), name);
79 }
80 }
81
82 return namespaces;
83 }
84} // namespace
85
86mllif::shared::Annotation::Annotation(const std::string &annotation) {
87 bool key = true;
88 std::stringstream ss{ annotation };
89 for (std::string term; std::getline(ss, term, Delimiter); key = false) {
90 if (key) {
91 Key = term;
92 } else {
93 Values.emplace_back(term);
94 }
95 }
96}
97
98void mllif::shared::CreateAnnotation(clang::FunctionDecl *decl) {
99 const auto isMethod = clang::dyn_cast<clang::CXXMethodDecl>(decl);
100 const std::vector type = {(isMethod ? type::Method : type::Function)};
101 ApplyAnnotation(decl, prefix::Type, type.data(), type.size());
102
103 const auto dirs = GetPath(decl);
104 ApplyAnnotation(decl, prefix::Path, dirs.data(), dirs.size());
105}
constexpr std::string Path
Definition annotation.h:30
constexpr std::string Type
Definition annotation.h:31
constexpr std::string Method
Definition annotation.h:36
constexpr std::string Function
Definition annotation.h:35
void CreateAnnotation(clang::FunctionDecl *decl)
Annotate a declaration with its information that may be lost.
constexpr std::string Namespace
Definition annotation.h:27
std::vector< std::string > Values
Definition annotation.h:42
Annotation(const std::string &annotation)