1 /* 2 * Copyright (c) 2023 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 ECMASCRIPT_COMPILER_TYPE_INFERENCE_INITIALIZATION_ANALYSIS_H 17 #define ECMASCRIPT_COMPILER_TYPE_INFERENCE_INITIALIZATION_ANALYSIS_H 18 19 #include "ecmascript/compiler/argument_accessor.h" 20 #include "ecmascript/compiler/circuit.h" 21 #include "ecmascript/compiler/gate_accessor.h" 22 #include "ecmascript/ts_types/ts_manager.h" 23 24 namespace panda::ecmascript::kungfu { 25 enum class ThisUsage : uint8_t { 26 INDEFINITE_THIS = 0, 27 DEFINITE_THIS, 28 }; 29 30 class InitializationAnalysis { 31 public: InitializationAnalysis(Circuit * circuit,TSManager * tsManager,const CString & recordName,const std::string & name,bool enableLog)32 InitializationAnalysis(Circuit *circuit, TSManager *tsManager, const CString &recordName, 33 const std::string &name, bool enableLog) 34 : tsManager_(tsManager), circuit_(circuit), acc_(circuit), recordName_(recordName), 35 methodName_(name), enableLog_(enableLog) 36 { 37 StoreThisObject(); 38 } 39 ~InitializationAnalysis() = default; 40 41 void Run(); 42 43 private: IsLogEnabled()44 inline bool IsLogEnabled() const 45 { 46 return enableLog_; 47 } 48 GetMethodName()49 inline const std::string &GetMethodName() const 50 { 51 return methodName_; 52 } 53 IsThisFromArg(GateRef gate)54 inline bool IsThisFromArg(GateRef gate) const 55 { 56 return gate == thisObject_; 57 } 58 59 void Analyse(GateRef gate); 60 void CollectInitializationInfo(GateRef gate, ThisUsage thisUsage); 61 void CollectInitializationType(GateRef gate, ThisUsage thisUsage); 62 bool CheckIsThisObject(GateRef receiver) const; 63 void MarkSuperClass(); 64 bool CheckSimpleCFG(GateRef gate, const uint16_t index) const; 65 bool CheckSimpleGate(GateRef gate, const uint16_t index) const; 66 bool CheckSimpleJSGate(GateRef gate, const uint16_t index) const; 67 bool CheckLdObjByName(GateRef gate, const uint16_t index, ThisUsage thisUsage) const; 68 bool CheckLdObjByIndexOrValue(GateRef gate) const; 69 bool CheckCall() const; 70 void StoreThisObject(); 71 bool CheckThisAsValueIn(GateRef gate) const; 72 void CollectThisEscapedInfo(GateRef gate); 73 bool HasEscapedThis(GateRef gate) const; 74 void MarkHasAnalysedInitialization(); 75 bool IsThisFromSuperCall(GateRef gate) const; 76 void Print() const; 77 78 TSManager *tsManager_ {nullptr}; 79 Circuit *circuit_ {nullptr}; 80 GateAccessor acc_; 81 GateRef thisObject_ {Circuit::NullGate()}; 82 GateType classType_ {GateType::AnyType()}; 83 const CString &recordName_; 84 const std::string &methodName_; 85 bool enableLog_ {false}; 86 }; 87 } // panda::ecmascript::kungfu 88 #endif // ECMASCRIPT_COMPILER_TYPE_INFERENCE_INITIALIZATION_ANALYSIS_H 89