1 /* 2 * Copyright (c) 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 #ifndef OHOS_IDL_METADATABUILDER_H 17 #define OHOS_IDL_METADATABUILDER_H 18 19 #include <memory> 20 #include "ast/ast.h" 21 #include "metadata/meta_component.h" 22 #include "util/autoptr.h" 23 #include "util/string_pool.h" 24 25 namespace OHOS { 26 namespace Idl { 27 class MetadataBuilder { 28 public: MetadataBuilder(AST * module)29 explicit MetadataBuilder(AST* module) 30 : module_(module) 31 {} 32 33 ~MetadataBuilder() = default; 34 35 std::shared_ptr<MetaComponent> Build(); 36 37 private: 38 size_t CalculateMetadataSize(); 39 40 void CalculateMetaComponent(AST* module); 41 42 void CalculateMetaNamespace(ASTNamespace* nspace); 43 44 void CalculateMetaSequenceable(ASTSequenceableType* sequenceable); 45 46 void CalculateMetaRawData(ASTRawDataType* rawdata); 47 48 void CalculateMetaInterface(ASTInterfaceType* interface); 49 50 void CalculateMetaMethod(ASTMethod* method); 51 52 void CalculateMetaParameter(ASTParameter* parameter); 53 54 void CalculateMetaType(ASTType* type); 55 56 void CalculateStringPool(); 57 58 void WriteMetadata(uintptr_t base); 59 60 void WriteMetaComponent(AST* module); 61 62 MetaNamespace* WriteMetaNamespace(ASTNamespace* nspace); 63 64 MetaSequenceable* WriteMetaSequenceable(ASTSequenceableType* parcelabe); 65 66 MetaRawData* WriteMetaRawData(ASTRawDataType* parcelabe); 67 68 MetaInterface* WriteMetaInterface(ASTInterfaceType* interface); 69 70 MetaMethod* WriteMetaMethod(ASTMethod* method); 71 72 MetaParameter* WriteMetaParameter(ASTParameter* parameter); 73 74 MetaType* WriteMetaType(ASTType* type); 75 76 char* WriteString(const std::string& string); 77 78 MetaTypeKind Type2Kind(ASTType* type); 79 80 static std::string tag_; 81 AutoPtr<AST> module_; 82 std::shared_ptr<MetaComponent> metaComponent_; 83 uintptr_t baseAddr_ = 0; 84 size_t size_ = 0; 85 StringPool stringPool_; 86 }; 87 } // namespace Idl 88 } // namespace OHOS 89 #endif // OHOS_IDL_METADATABUILDER_H 90