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