1 /** 2 * Copyright (c) 2021-2022 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 #ifndef ES2PANDA_COMPILER_CORE_COMPILER_CONTEXT_H 17 #define ES2PANDA_COMPILER_CORE_COMPILER_CONTEXT_H 18 19 #include <macros.h> 20 #include <mem/arena_allocator.h> 21 22 #include <binder/variable.h> 23 #include <ir/astNode.h> 24 #include <util/patchFix.h> 25 #include <util/ustring.h> 26 27 #include <cstdint> 28 #include <mutex> 29 30 namespace panda::es2panda::binder { 31 class Binder; 32 } // namespace panda::es2panda::binder 33 34 namespace panda::es2panda::extractor { 35 class TypeRecorder; 36 class TypeExtractor; 37 } // namespace panda::es2panda::extractor 38 39 namespace panda::es2panda::compiler { 40 41 class DebugInfo; 42 class Emitter; 43 44 class CompilerContext { 45 public: 46 CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, 47 bool isMergeAbc, bool isTypeExtractorEnabled, bool isJsonInputFile, bool isRecordSource, 48 std::string sourceFile, std::string pkgName, util::StringView recordName, 49 util::PatchFix *patchFixHelper); 50 51 NO_COPY_SEMANTIC(CompilerContext); 52 NO_MOVE_SEMANTIC(CompilerContext); 53 ~CompilerContext() = default; 54 Binder()55 binder::Binder *Binder() const 56 { 57 return binder_; 58 } 59 GetEmitter()60 Emitter *GetEmitter() const 61 { 62 return emitter_.get(); 63 } 64 LiteralCount()65 int32_t LiteralCount() const 66 { 67 return literalBufferIdx_; 68 } 69 NewLiteralIndex()70 int32_t NewLiteralIndex() 71 { 72 std::lock_guard lock(m_); 73 return literalBufferIdx_++; 74 } 75 Mutex()76 std::mutex &Mutex() 77 { 78 return m_; 79 } 80 IsDebug()81 bool IsDebug() const 82 { 83 return isDebug_; 84 } 85 IsDebuggerEvaluateExpressionMode()86 bool IsDebuggerEvaluateExpressionMode() const 87 { 88 return isDebuggerEvaluateExpressionMode_; 89 } 90 IsMergeAbc()91 bool IsMergeAbc() const 92 { 93 return isMergeAbc_; 94 } 95 SourceFile()96 std::string SourceFile() const 97 { 98 return sourceFile_; 99 } 100 PkgName()101 std::string PkgName() const 102 { 103 return pkgName_; 104 } 105 PatchFixHelper()106 util::PatchFix *PatchFixHelper() const 107 { 108 return patchFixHelper_; 109 } 110 RecordName()111 const util::StringView &RecordName() const 112 { 113 return recordName_; 114 } 115 IsTypeExtractorEnabled()116 bool IsTypeExtractorEnabled() const 117 { 118 return isTypeExtractorEnabled_; 119 } 120 TypeRecorder()121 extractor::TypeRecorder *TypeRecorder() const 122 { 123 return recorder_; 124 } 125 TypeExtractor()126 extractor::TypeExtractor *TypeExtractor() const 127 { 128 return extractor_; 129 } 130 131 void SetTypeRecorder(extractor::TypeRecorder *recorder); 132 void SetTypeExtractor(extractor::TypeExtractor *extractor); 133 IsJsonInputFile()134 bool IsJsonInputFile() const 135 { 136 return isJsonInputFile_; 137 } 138 IsRecordSource()139 bool IsRecordSource() const 140 { 141 return isRecordSource_; 142 } 143 144 private: 145 binder::Binder *binder_; 146 int32_t literalBufferIdx_ {0}; 147 std::mutex m_; 148 bool isDebug_; 149 bool isDebuggerEvaluateExpressionMode_; 150 bool isMergeAbc_; 151 // For Type Extractor 152 bool isTypeExtractorEnabled_; 153 // true when input file is json file 154 bool isJsonInputFile_; 155 bool isRecordSource_; 156 extractor::TypeRecorder *recorder_ {}; 157 extractor::TypeExtractor *extractor_ {}; 158 std::string sourceFile_; 159 std::string pkgName_; 160 util::StringView recordName_; 161 util::PatchFix *patchFixHelper_ {nullptr}; 162 std::unique_ptr<Emitter> emitter_; 163 }; 164 165 } // namespace panda::es2panda::compiler 166 167 #endif 168