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 "es2panda.h" 17 18#include "compiler/core/compilerImpl.h" 19#include "util/options.h" 20 21#include <iostream> 22#include <thread> 23 24namespace ark::es2panda { 25constexpr size_t DEFAULT_THREAD_COUNT = 2; 26 27template <class T> 28T DirName(T const &path, T const &delims = ark::os::file::File::GetPathDelim()) 29{ 30 std::size_t pos = path.find_last_of(delims); 31 if (pos == std::string::npos) { 32 return "./"; 33 } 34 35 if (pos == 0) { 36 return delims; 37 } 38 39 std::string_view dirPath = path.substr(0, pos); 40 if (dirPath.compare(".") == 0 || dirPath.compare("..") == 0) { 41 return path.substr(0, pos + 1); 42 } 43 44 return path.substr(0, pos); 45} 46 47SourceFile::SourceFile(std::string_view fn, std::string_view s) : filePath(fn), fileFolder(DirName(fn)), source(s) {} 48 49SourceFile::SourceFile(std::string_view fn, std::string_view s, bool m) 50 : filePath(fn), fileFolder(DirName(fn)), source(s), isModule(m) 51{ 52} 53 54SourceFile::SourceFile(std::string_view fn, std::string_view s, std::string_view rp, bool m) 55 : filePath(fn), fileFolder(DirName(fn)), source(s), resolvedPath(DirName(rp)), isModule(m) 56{ 57} 58 59Compiler::Compiler(ScriptExtension ext) : Compiler(ext, DEFAULT_THREAD_COUNT, {}) {} 60 61Compiler::Compiler(ScriptExtension ext, size_t threadCount) : Compiler(ext, threadCount, {}) {} 62 63Compiler::Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins) 64 : plugins_(std::move(plugins)), compiler_(new compiler::CompilerImpl(threadCount, &plugins_)), ext_(ext) 65{ 66} 67 68Compiler::~Compiler() 69{ 70 delete compiler_; 71} 72 73pandasm::Program *Compiler::Compile(const SourceFile &input, const util::Options &options, uint32_t parseStatus) 74{ 75 try { 76 return compiler_->Compile(compiler::CompilationUnit {input, options, parseStatus, ext_}); 77 } catch (const class Error &e) { 78 error_ = e; 79 return nullptr; 80 } 81} 82 83bool Compiler::IsAnyError() const noexcept 84{ 85 return compiler_->IsAnyError(); 86} 87 88std::string Compiler::GetPhasesList() const 89{ 90 return compiler::CompilerImpl::GetPhasesList(ext_); 91} 92 93void Compiler::DumpAsm(const pandasm::Program *prog) 94{ 95 compiler::CompilerImpl::DumpAsm(prog); 96} 97} // namespace ark::es2panda 98