1/**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "runtime/core/core_language_context.h"
17
18% Common::each_plugin_option "lang_context_header_path" do |lang_context_header_path|
19#include "<%= lang_context_header_path %>"
20% end
21
22namespace ark::plugins {
23
24LanguageContextBase* GetLanguageContextBase([[maybe_unused]] ark::panda_file::SourceLang lang)
25{
26% Common::each_plugin_option "lang_context_class" do |lang_context_class, _, plugin_opts|
27if (lang == <%= plugin_opts["lang_enum"] %>)
28    {
29        static <%= lang_context_class %> ctx;
30        return &ctx;
31    }
32% end
33
34   static CoreLanguageContext coreCtx;
35   return &coreCtx;
36}
37
38panda_file::SourceLang RuntimeTypeToLang(const std::string &runtimeType) {
39    if (runtimeType == "core") {
40        return panda_file::SourceLang::PANDA_ASSEMBLY;
41    }
42
43% Common::plugins.each do |plugin_lang, plugin_opts|
44    if (runtimeType == "<%= plugin_lang.downcase %>") {
45        return <%= plugin_opts["lang_enum"] %>;
46    }
47% end
48
49    LOG(FATAL, RUNTIME) << "Incorrect runtime_type: " << runtimeType;
50    UNREACHABLE();
51    return ark::panda_file::SourceLang::PANDA_ASSEMBLY;
52}
53
54std::string_view LangToRuntimeType(panda_file::SourceLang lang) {
55    if (lang == panda_file::SourceLang::PANDA_ASSEMBLY) {
56        return "core";
57    }
58
59% Common::plugins.each do |plugin_lang, plugin_opts|
60    if (lang == <%= plugin_opts["lang_enum"] %>) {
61        return "<%= plugin_lang.downcase %>";
62    }
63% end
64
65    LOG(FATAL, RUNTIME) << "Incorrect lang: " << lang;
66    UNREACHABLE();
67    return "core";
68}
69
70bool HasRuntime(const std::string &runtimeType) {
71    if (runtimeType == "core") {
72        return true; // NOLINT(readability-simplify-boolean-expr)
73    }
74
75% Common::plugins.each_key do |plugin_lang|
76    if (runtimeType == "<%= plugin_lang.downcase %>") {
77        return true; // NOLINT(readability-simplify-boolean-expr)
78    }
79% end
80
81    return false;
82}
83
84}  // namespace ark::plugins
85