• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace panda::es2panda {
25 constexpr size_t DEFAULT_THREAD_COUNT = 2;
26 
27 template <class T>
DirName(T const & path,T const & delims=panda::os::file::File::GetPathDelim ())28 T DirName(T const &path, T const &delims = panda::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 
SourceFile(std::string_view fn,std::string_view s)47 SourceFile::SourceFile(std::string_view fn, std::string_view s) : filePath(fn), fileFolder(DirName(fn)), source(s) {}
48 
SourceFile(std::string_view fn,std::string_view s,bool m)49 SourceFile::SourceFile(std::string_view fn, std::string_view s, bool m)
50     : filePath(fn), fileFolder(DirName(fn)), source(s), isModule(m)
51 {
52 }
53 
SourceFile(std::string_view fn,std::string_view s,std::string_view rp,bool m)54 SourceFile::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 
Compiler(ScriptExtension ext)59 Compiler::Compiler(ScriptExtension ext) : Compiler(ext, DEFAULT_THREAD_COUNT, {}) {}
60 
Compiler(ScriptExtension ext,size_t threadCount)61 Compiler::Compiler(ScriptExtension ext, size_t threadCount) : Compiler(ext, threadCount, {}) {}
62 
Compiler(ScriptExtension ext,size_t threadCount,std::vector<util::Plugin> && plugins)63 Compiler::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 
~Compiler()68 Compiler::~Compiler()
69 {
70     delete compiler_;
71 }
72 
Compile(const SourceFile & input,const CompilerOptions & options,uint32_t parseStatus)73 pandasm::Program *Compiler::Compile(const SourceFile &input, const CompilerOptions &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 
DumpAsm(const pandasm::Program * prog)83 void Compiler::DumpAsm(const pandasm::Program *prog)
84 {
85     compiler::CompilerImpl::DumpAsm(prog);
86 }
87 }  // namespace panda::es2panda
88