MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
BridgeGen.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
19
20bool mllif::BridgeGen::handleDecl(MLLIFContext &context, const std::shared_ptr<Decl> &node, std::ostream &out, std::size_t indent) {
21#define CHILD_OF(t) std::dynamic_pointer_cast<t##Decl>(child)
22#define NESTED_DECL(t, cond, msg, I, ...) \
23 if (const auto decl = std::dynamic_pointer_cast<t##Decl>(node)) { \
24 if (!handle##t##Begin(context, *decl, out, indent)) { \
25 return false; \
26 } \
27 \
28 for (auto i = 0; i < decl->children().size(); ++i) { \
29 auto &child = decl->children()[i]; \
30 if (cond) { \
31 context.error(msg); \
32 continue; \
33 } \
34 if (!handleDecl(context, child, out, indent + 1 + I)) { \
35 continue; \
36 } \
37 __VA_ARGS__ \
38 } \
39 \
40 if (!handle##t##End(context, *decl, out, indent)) { \
41 return false; \
42 } \
43 }
44
45 NESTED_DECL(Assembly, false, "", -1)
46 else NESTED_DECL(Namespace, CHILD_OF(Assembly), "AssemblyDecl cannot be the child of NamespaceDecl", 0)
47 else NESTED_DECL(Object, CHILD_OF(Namespace) || CHILD_OF(Assembly), "AssemblyDecl or NamespaceDecl cannot be the child of ObjectDecl", 0)
48 else NESTED_DECL(
49 Method, !CHILD_OF(Param),
50 "Only ParamDecl can be the child of MethodDecl", 0,
51 if (i != decl->children().size() - 1) { writeParamDelimiter(out); })
52 else NESTED_DECL(
53 Function, !CHILD_OF(Param),
54 "Only ParamDecl can be the child of FunctionDecl", 0,
55 if (i != decl->children().size() - 1) { writeParamDelimiter(out); })
56 else if (const auto decl = std::dynamic_pointer_cast<ParamDecl>(node)) {
57 if (!handleParam(context, *decl, out, indent)) {
58 return false;
59 }
60 if (decl->children().size()) {
61 context.error("ParamDecl cannot have any children");
62 return false;
63 }
64 }
65
66 return true;
67}
#define CHILD_OF(t)
#define NESTED_DECL(t, cond, msg, I,...)
bool handleDecl(MLLIFContext &context, const std::shared_ptr< Decl > &node, std::ostream &out, std::size_t indent)
Definition BridgeGen.cxx:20
void error(const std::string &what)
Definition Context.h:35