1 /* 2 * Copyright (c) 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 ECMASCRIPT_COMPILER_TYPE_RECORDER_H 17 #define ECMASCRIPT_COMPILER_TYPE_RECORDER_H 18 19 #include "ecmascript/compiler/type.h" 20 #include "ecmascript/jspandafile/js_pandafile.h" 21 #include "ecmascript/method.h" 22 #include "ecmascript/ts_types/ts_manager.h" 23 24 namespace panda::ecmascript::kungfu { 25 enum class TypedArgIdx : uint8_t { 26 FUNC = 0, 27 NEW_TARGET, 28 THIS_OBJECT, 29 NUM_OF_TYPED_ARGS, 30 }; 31 32 class TypeRecorder { 33 public: 34 explicit TypeRecorder(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral, 35 TSManager *tsManager, const CString &recordName); 36 ~TypeRecorder() = default; 37 38 GateType GetType(const int32_t offset) const; 39 GateType GetArgType(const uint32_t argIndex) const; 40 GateType UpdateType(const int32_t offset, const GateType &type) const; 41 42 static constexpr int METHOD_ANNOTATION_THIS_TYPE_OFFSET = -2; 43 44 private: 45 static constexpr int METHOD_ANNOTATION_FUNCTION_TYPE_OFFSET = -1; 46 47 void LoadTypes(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral, 48 TSManager *tsManager, const CString &recordName); 49 50 void LoadArgTypes(const TSManager *tsManager, GlobalTSTypeRef funcGT, GlobalTSTypeRef thisGT); 51 52 // This function tries to get the type of 'this'. In success, the type of the class is returned if the function is 53 // a static member, or the type of instances of the class is returned. Otherwise the type 'any' is returned. 54 GateType TryGetThisType(const TSManager *tsManager, GlobalTSTypeRef funcGT, GlobalTSTypeRef thisGT) const; 55 56 // This function tries to get the type of 'newTarget'. In success, the type of the class is returned. Otherwise, 57 // the type 'any' is returned. 58 GateType TryGetNewTargetType(const TSManager *tsManager, GlobalTSTypeRef thisGT) const; 59 60 // This function tries to get the type of the function. On failure, the type 'any' is returned. 61 GateType TryGetFuncType(GlobalTSTypeRef funcGT) const; 62 63 std::unordered_map<int32_t, GateType> bcOffsetGtMap_ {}; 64 std::vector<GateType> argTypes_; 65 }; 66 } // panda::ecmascript::kungfu 67 #endif // ECMASCRIPT_COMPILER_TYPE_RECORDER_H 68