MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
ExportAttr.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2024 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"
20
22 // NOTE: `S` should be `static` (experimental proof on Clang/LLVM 20.0)
23 static constexpr std::array<Spelling, 3> S = {{
24 {
25 .Syntax = clang::ParsedAttr::AS_GNU,
26 .NormalizedFullName = "mllif_export",
27 },
28 {
29 .Syntax = clang::ParsedAttr::AS_C23,
30 .NormalizedFullName = "mllif::export",
31 },
32 {
33 .Syntax = clang::ParsedAttr::AS_CXX11,
34 .NormalizedFullName = "mllif::export",
35 },
36 }};
37 Spellings = S;
38}
39
40auto mllif::c::ExportAttrInfo::diagAppertainsToDecl(clang::Sema &, const clang::ParsedAttr &, const clang::Decl *D) const -> bool {
41 return clang::dyn_cast<clang::FunctionDecl>(D) || clang::dyn_cast<clang::CXXRecordDecl>(D);
42}
43
44auto mllif::c::ExportAttrInfo::diagAppertainsToStmt(clang::Sema &, const clang::ParsedAttr &, const clang::Stmt *) const -> bool {
45 return false;
46}
47
48auto mllif::c::ExportAttrInfo::handleDeclAttribute(clang::Sema &S, clang::Decl *D, const clang::ParsedAttr &/**/) const -> AttrHandling {
49 if (const auto record = clang::dyn_cast<clang::CXXRecordDecl>(D)) {
50 for (const auto method : record->methods()) {
51 if (method->getVisibility() != clang::Visibility::HiddenVisibility) {
53 }
54 }
55 } else if (const auto fn = clang::dyn_cast<clang::FunctionDecl>(D)) {
56 shared::CreateAnnotation(fn); // just mark a symbol to process it later
57 } else {
58 const auto id = S.Diags.getCustomDiagID(
59 clang::DiagnosticsEngine::Warning,
60 "%0 '%1' can not be exported; Only function can be exported. "
61 "If you want to export records such as class or struct, You can make a function that depends on these records");
62
63 std::string name;
64 if (const auto named = clang::dyn_cast<clang::NamedDecl>(D)) {
65 name = named->getName();
66 } else {
67 name = std::to_string(D->getID());
68 }
69
70 S.Diag(D->getLocation(), id) << D->getDeclKindName() << name;
71
72 return AttributeNotApplied;
73 }
74
75 return AttributeApplied;
76}
77
78namespace {
79 [[maybe_unused]]
80 //NOLINTNEXTLINE(cert-err58-cpp) : Initialization of 'Y' with static storage duration may throw an exception that cannot be caught
81 const clang::ParsedAttrInfoRegistry::Add<mllif::c::ExportAttrInfo> Y("mllif", "");
82}
auto diagAppertainsToDecl(clang::Sema &, const clang::ParsedAttr &, const clang::Decl *D) const -> bool override
auto handleDeclAttribute(clang::Sema &S, clang::Decl *D, const clang::ParsedAttr &) const -> AttrHandling override
auto diagAppertainsToStmt(clang::Sema &, const clang::ParsedAttr &, const clang::Stmt *) const -> bool override
void CreateAnnotation(clang::FunctionDecl *decl)
Annotate a declaration with its information that may be lost.