13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "es2panda.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include "compiler/core/compilerImpl.h"
193af6ab5fSopenharmony_ci#include "util/options.h"
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ci#include <iostream>
223af6ab5fSopenharmony_ci#include <thread>
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_cinamespace ark::es2panda {
253af6ab5fSopenharmony_ciconstexpr size_t DEFAULT_THREAD_COUNT = 2;
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_citemplate <class T>
283af6ab5fSopenharmony_ciT DirName(T const &path, T const &delims = ark::os::file::File::GetPathDelim())
293af6ab5fSopenharmony_ci{
303af6ab5fSopenharmony_ci    std::size_t pos = path.find_last_of(delims);
313af6ab5fSopenharmony_ci    if (pos == std::string::npos) {
323af6ab5fSopenharmony_ci        return "./";
333af6ab5fSopenharmony_ci    }
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_ci    if (pos == 0) {
363af6ab5fSopenharmony_ci        return delims;
373af6ab5fSopenharmony_ci    }
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci    std::string_view dirPath = path.substr(0, pos);
403af6ab5fSopenharmony_ci    if (dirPath.compare(".") == 0 || dirPath.compare("..") == 0) {
413af6ab5fSopenharmony_ci        return path.substr(0, pos + 1);
423af6ab5fSopenharmony_ci    }
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_ci    return path.substr(0, pos);
453af6ab5fSopenharmony_ci}
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_ciSourceFile::SourceFile(std::string_view fn, std::string_view s) : filePath(fn), fileFolder(DirName(fn)), source(s) {}
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ciSourceFile::SourceFile(std::string_view fn, std::string_view s, bool m)
503af6ab5fSopenharmony_ci    : filePath(fn), fileFolder(DirName(fn)), source(s), isModule(m)
513af6ab5fSopenharmony_ci{
523af6ab5fSopenharmony_ci}
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ciSourceFile::SourceFile(std::string_view fn, std::string_view s, std::string_view rp, bool m)
553af6ab5fSopenharmony_ci    : filePath(fn), fileFolder(DirName(fn)), source(s), resolvedPath(DirName(rp)), isModule(m)
563af6ab5fSopenharmony_ci{
573af6ab5fSopenharmony_ci}
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ciCompiler::Compiler(ScriptExtension ext) : Compiler(ext, DEFAULT_THREAD_COUNT, {}) {}
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ciCompiler::Compiler(ScriptExtension ext, size_t threadCount) : Compiler(ext, threadCount, {}) {}
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ciCompiler::Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins)
643af6ab5fSopenharmony_ci    : plugins_(std::move(plugins)), compiler_(new compiler::CompilerImpl(threadCount, &plugins_)), ext_(ext)
653af6ab5fSopenharmony_ci{
663af6ab5fSopenharmony_ci}
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_ciCompiler::~Compiler()
693af6ab5fSopenharmony_ci{
703af6ab5fSopenharmony_ci    delete compiler_;
713af6ab5fSopenharmony_ci}
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_cipandasm::Program *Compiler::Compile(const SourceFile &input, const util::Options &options, uint32_t parseStatus)
743af6ab5fSopenharmony_ci{
753af6ab5fSopenharmony_ci    try {
763af6ab5fSopenharmony_ci        return compiler_->Compile(compiler::CompilationUnit {input, options, parseStatus, ext_});
773af6ab5fSopenharmony_ci    } catch (const class Error &e) {
783af6ab5fSopenharmony_ci        error_ = e;
793af6ab5fSopenharmony_ci        return nullptr;
803af6ab5fSopenharmony_ci    }
813af6ab5fSopenharmony_ci}
823af6ab5fSopenharmony_ci
833af6ab5fSopenharmony_cibool Compiler::IsAnyError() const noexcept
843af6ab5fSopenharmony_ci{
853af6ab5fSopenharmony_ci    return compiler_->IsAnyError();
863af6ab5fSopenharmony_ci}
873af6ab5fSopenharmony_ci
883af6ab5fSopenharmony_cistd::string Compiler::GetPhasesList() const
893af6ab5fSopenharmony_ci{
903af6ab5fSopenharmony_ci    return compiler::CompilerImpl::GetPhasesList(ext_);
913af6ab5fSopenharmony_ci}
923af6ab5fSopenharmony_ci
933af6ab5fSopenharmony_civoid Compiler::DumpAsm(const pandasm::Program *prog)
943af6ab5fSopenharmony_ci{
953af6ab5fSopenharmony_ci    compiler::CompilerImpl::DumpAsm(prog);
963af6ab5fSopenharmony_ci}
973af6ab5fSopenharmony_ci}  // namespace ark::es2panda
98