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 CalculateMetaInterface(ASTInterfaceType* interface);
47 
48     void CalculateMetaMethod(ASTMethod* method);
49 
50     void CalculateMetaParameter(ASTParameter* parameter);
51 
52     void CalculateMetaType(ASTType* type);
53 
54     void CalculateStringPool();
55 
56     void WriteMetadata(uintptr_t base);
57 
58     void WriteMetaComponent(AST* module);
59 
60     MetaNamespace* WriteMetaNamespace(ASTNamespace* nspace);
61 
62     MetaSequenceable* WriteMetaSequenceable(ASTSequenceableType* parcelabe);
63 
64     MetaInterface* WriteMetaInterface(ASTInterfaceType* interface);
65 
66     MetaMethod* WriteMetaMethod(ASTMethod* method);
67 
68     MetaParameter* WriteMetaParameter(ASTParameter* parameter);
69 
70     MetaType* WriteMetaType(ASTType* type);
71 
72     char* WriteString(const std::string& string);
73 
74     MetaTypeKind Type2Kind(ASTType* type);
75 
76     static std::string tag_;
77     AutoPtr<AST> module_;
78     std::shared_ptr<MetaComponent> metaComponent_;
79     uintptr_t baseAddr_ = 0;
80     size_t size_ = 0;
81     StringPool stringPool_;
82 };
83 } // namespace Idl
84 } // namespace OHOS
85 #endif // OHOS_IDL_METADATABUILDER_H
86