工具合一hdi类型定义部分上库

Signed-off-by: millerluo <luoxueting2@huawei.com>
Change-Id: Idd79b0c19144c64fcada44cf2afb5c05c72f0f0f
This commit is contained in:
millerluo 2024-05-15 18:11:36 +08:00
parent c80c144c9a
commit af8904e038
47 changed files with 7049 additions and 2 deletions

View File

@ -35,9 +35,12 @@ OpenHarmony IDL接口描述语言主要用于
```
foundation/ability/idl_tool
├── ast # idl语法解析定义代码
├── codegen # 跨进程通信模板生成模块代码
├── codegen # 模板生成模块代码
├── hash # hash生成模块代码
├── lexer # 词法解析模块代码
├── metadata # matedata自定义数据解析模块代码
├── parser # idl解析模块代码
├── parser # 语法解析模块代码
├── preprocessor # 预处理模块代码
├── test # 测试目录
└── util # 公共方法代码
```

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,163 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_ARRAYTYPE_EMITTER_H
#define OHOS_IDL_HDI_ARRAYTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiArrayTypeEmitter : public HdiTypeEmitter {
public:
inline void SetElementEmitter(const AutoPtr<HdiTypeEmitter> &elementEmitter)
{
elementEmitter_ = elementEmitter;
}
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyWriteOutVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadOutVar(const std::string &memFlagName, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName,
const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const override;
// c methods about reading and writing array with pod element
void EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCStubReadMethodBody(StringBuilder &sb, const std::string &prefix) const;
// c methods about reading and writing string array
void EmitCWriteStrArrayMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const;
void EmitCReadStrArrayMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const;
void EmitCReadStrArrayMethodBody(StringBuilder &sb, const std::string &prefix) const;
void EmitCCheckParamOfReadStringArray(StringBuilder &sb, const std::string &prefix) const;
void EmitCStubReadStrArrayMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const;
void EmitCStubReadStrArrayMethodBody(StringBuilder &sb, const std::string &prefix) const;
void EmitCStubReadStrArrayFree(StringBuilder &sb, const std::string &prefix) const;
// cpp methods about reading and writing array with pod element
void EmitCppWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCppReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
protected:
void EmitJavaWriteArrayVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const;
void EmitCMallocVar(const std::string &name, const std::string &lenName,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
void EmitCStringElementUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &newPrefix, std::vector<std::string> &freeObjStatements) const;
void EmitCElementUnMarshallingReadLen(const std::string &lenName, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const;
void EmitCReadMethodsBody(StringBuilder &sb, const std::string &prefix) const;
AutoPtr<HdiTypeEmitter> elementEmitter_;
};
class HdiListTypeEmitter : public HdiArrayTypeEmitter {
public:
HdiListTypeEmitter() : HdiArrayTypeEmitter()
{
isPod_ = false;
}
TypeKind GetTypeKind() override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_ARRAYTYPE_EMITTER_H

View File

@ -0,0 +1,176 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_boolean_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiBooleanTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_BOOLEAN;
}
std::string HdiBooleanTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "bool";
case TypeMode::PARAM_IN:
return "bool";
case TypeMode::PARAM_OUT:
return "bool*";
case TypeMode::LOCAL_VAR:
return "bool";
default:
return "unknow type";
}
}
std::string HdiBooleanTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "bool";
case TypeMode::PARAM_IN:
return "bool";
case TypeMode::PARAM_OUT:
return "bool&";
case TypeMode::LOCAL_VAR:
return "bool";
default:
return "unknow type";
}
}
std::string HdiBooleanTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Boolean" : "boolean";
}
void HdiBooleanTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt8(%s, %s ? 1 : 0)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(%s, (int8_t *)%s)) {\n",
replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(%s, (int8_t *)%s)) {\n",
dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteBool(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel)const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = false;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadBool(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt8(data, %s ? 1 : 0)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(data, (int8_t *)&%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteBool(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = false;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadBool(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiBooleanTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeBoolean(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiBooleanTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readBoolean();\n", name.c_str(), parcelName.c_str());
}
void HdiBooleanTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readBoolean();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_BOOLEANTYPE_EMITTER_H
#define OHOS_IDL_HDI_BOOLEANTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiBooleanTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_BOOLEANTYPE_EMITTER_H

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_byte_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiByteTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_BYTE;
}
std::string HdiByteTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int8_t";
case TypeMode::PARAM_IN:
return "int8_t";
case TypeMode::PARAM_OUT:
return "int8_t*";
case TypeMode::LOCAL_VAR:
return "int8_t";
default:
return "unknow type";
}
}
std::string HdiByteTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int8_t";
case TypeMode::PARAM_IN:
return "int8_t";
case TypeMode::PARAM_OUT:
return "int8_t&";
case TypeMode::LOCAL_VAR:
return "int8_t";
default:
return "unknow type";
}
}
std::string HdiByteTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Byte" : "byte";
}
void HdiByteTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt8(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt8(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt8(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiByteTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeByte(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiByteTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readByte();\n", name.c_str(), parcelName.c_str());
}
void HdiByteTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readByte();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_BYTETYPE_EMITTER_H
#define OHOS_IDL_HDI_BYTETYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiByteTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_BYTETYPE_EMITTER_H

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_double_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiDoubleTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_DOUBLE;
}
std::string HdiDoubleTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "double";
case TypeMode::PARAM_IN:
return "double";
case TypeMode::PARAM_OUT:
return "double*";
case TypeMode::LOCAL_VAR:
return "double";
default:
return "unknow type";
}
}
std::string HdiDoubleTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "double";
case TypeMode::PARAM_IN:
return "double";
case TypeMode::PARAM_OUT:
return "double&";
case TypeMode::LOCAL_VAR:
return "double";
default:
return "unknow type";
}
}
std::string HdiDoubleTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Double" : "double";
}
void HdiDoubleTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteDouble(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadDouble(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadDouble(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteDouble(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadDouble(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteDouble(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadDouble(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteDouble(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadDouble(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiDoubleTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeDouble(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiDoubleTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readDouble();\n", name.c_str(), parcelName.c_str());
}
void HdiDoubleTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readDouble();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_DOUBLETYPE_EMITTER_H
#define OHOS_IDL_HDI_DOUBLETYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiDoubleTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_DOUBLETYPE_EMITTER_H

View File

@ -0,0 +1,236 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_enum_type_emitter.h"
namespace OHOS {
namespace Idl {
void HdiEnumTypeEmitter::SetBaseTypeName(const std::string &name)
{
baseTypeName_ = name;
}
TypeKind HdiEnumTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_ENUM;
}
std::string HdiEnumTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("enum %s", name_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("enum %s", name_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("enum %s*", name_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("enum %s", name_.c_str());
default:
return "unknow type";
}
}
std::string HdiEnumTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
case TypeMode::PARAM_IN:
case TypeMode::LOCAL_VAR:
return StringHelper::Format("%s", typeName_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("%s&", typeName_.c_str());
default:
return "unknow type";
}
}
std::string HdiEnumTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// currently, Java does not support the enum type.
return "/";
}
std::string HdiEnumTypeEmitter::EmitCTypeDecl() const
{
StringBuilder sb;
sb.AppendFormat("enum %s {\n", name_.c_str());
for (auto it : members_) {
if (it->GetValue().empty()) {
sb.Append(TAB).AppendFormat("%s,\n", it->GetValueName().c_str());
} else {
sb.Append(TAB).AppendFormat("%s = %s,\n", it->GetValueName().c_str(), it->GetValue().c_str());
}
}
sb.Append("};");
return sb.ToString();
}
std::string HdiEnumTypeEmitter::EmitCppTypeDecl() const
{
StringBuilder sb;
if (baseTypeName_.empty()) {
sb.AppendFormat("enum %s {\n", name_.c_str());
} else {
sb.AppendFormat("enum %s : %s {\n", name_.c_str(), baseTypeName_.c_str());
}
for (auto it : members_) {
if (it->GetValue().empty()) {
sb.Append(TAB).AppendFormat("%s,\n", it->GetValueName().c_str());
} else {
sb.Append(TAB).AppendFormat("%s = %s,\n", it->GetValueName().c_str(), it->GetValue().c_str());
}
}
sb.Append("};");
return sb.ToString();
}
void HdiEnumTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint64(%s, (uint64_t)%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
std::string tmpVarName = "enumTmp";
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).AppendFormat("uint64_t %s = 0;\n", tmpVarName.c_str());
sb.Append(prefix + TAB)
.AppendFormat("if (!HdfSbufReadUint64(%s, &%s)) {\n", replyParcelName_.c_str(), tmpVarName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: invlid parameter %s\", __func__);\n",
name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).AppendFormat("*%s = (%s)%s;\n", name.c_str(), EmitCType().c_str(), tmpVarName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
std::string tmpVarName = "enumTmp";
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).AppendFormat("uint64_t %s = 0;\n", tmpVarName.c_str());
sb.Append(prefix + TAB)
.AppendFormat("if (!HdfSbufReadUint64(%s, &%s)) {\n", dataParcelName_.c_str(), tmpVarName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).AppendFormat("%s = (%s)%s;\n", name.c_str(), EmitCType().c_str(), tmpVarName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint64(static_cast<uint64_t>(%s))) {\n", parcelName.c_str(),
name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string tmpVarName = "enumTmp";
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = static_cast<%s>(0);\n", EmitCppType().c_str(), name.c_str(),
EmitCType().c_str());
}
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).AppendFormat("uint64_t %s = 0;\n", tmpVarName.c_str());
sb.Append(prefix + TAB).AppendFormat("if (!%s.ReadUint64(%s)) {\n", parcelName.c_str(), tmpVarName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n",
name.c_str());
sb.Append(prefix + TAB + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).AppendFormat("%s = static_cast<%s>(%s);\n", name.c_str(), EmitCType().c_str(),
tmpVarName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint64(data, (uint64_t)%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
std::string tmpVarName = "enumTmp";
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).AppendFormat("uint64_t %s = 0;\n", tmpVarName.c_str());
sb.Append(prefix + TAB).AppendFormat("if (!HdfSbufReadUint64(data, &%s)) {\n", tmpVarName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).AppendFormat("%s = (%s)%s;\n", name.c_str(), EmitCType().c_str(), tmpVarName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (!%s.WriteUint64(static_cast<uint64_t>(%s))) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiEnumTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
std::string tmpVarName = "enumTmp";
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = static_cast<%s>(0);\n", EmitCppType().c_str(), name.c_str(),
EmitCType().c_str());
}
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).AppendFormat("uint64_t %s = 0;\n", tmpVarName.c_str());
sb.Append(prefix + TAB).AppendFormat("if (!%s.ReadUint64(%s)) {\n", parcelName.c_str(), tmpVarName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n",
name.c_str());
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).AppendFormat("%s = static_cast<%s>(%s);\n", name.c_str(), EmitCType().c_str(),
tmpVarName.c_str());
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,97 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_ENUMTYPE_EMITTER_H
#define OHOS_IDL_HDI_ENUMTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiEnumValueEmitter : public HdiTypeEmitter {
public:
explicit HdiEnumValueEmitter(const std::string &name, const std::string &value) : mName_(name), value_(value) {}
inline ~HdiEnumValueEmitter() override {}
inline std::string GetValue()
{
return value_;
}
inline std::string GetValueName()
{
return mName_;
}
private:
std::string mName_;
std::string value_;
};
class HdiEnumTypeEmitter : public HdiTypeEmitter {
public:
inline void AddMember(const AutoPtr<HdiEnumValueEmitter> &member)
{
members_.push_back(member);
}
void SetBaseTypeName(const std::string &name);
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
std::string EmitCTypeDecl() const override;
std::string EmitCppTypeDecl() const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
private:
std::vector<AutoPtr<HdiEnumValueEmitter>> members_;
std::string baseTypeName_;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_ENUMTYPE_EMITTER_H

View File

@ -0,0 +1,319 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_fd_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiFdTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_FILEDESCRIPTOR;
}
std::string HdiFdTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int";
case TypeMode::PARAM_IN:
return "int";
case TypeMode::PARAM_OUT:
return "int*";
case TypeMode::LOCAL_VAR:
return "int";
default:
return "unknow type";
}
}
std::string HdiFdTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int";
case TypeMode::PARAM_IN:
return "int";
case TypeMode::PARAM_OUT:
return "int&";
case TypeMode::LOCAL_VAR:
return "int";
default:
return "unknow type";
}
}
std::string HdiFdTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Integer" : "int";
}
void HdiFdTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!WriteFileDescriptor(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
if (isInnerType) {
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(%s, &%s)) {\n", replyParcelName_.c_str(), name.c_str());
} else {
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(%s, &%s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!WriteFileDescriptor(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = -1;\n", EmitCppType().c_str(), name.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = -1;\n", name.c_str());
}
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).AppendFormat("}\n");
}
void HdiFdTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!WriteFileDescriptor(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!WriteFileDescriptor(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFdTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = -1;\n", EmitCppType().c_str(), name.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = -1;\n", name.c_str());
}
sb.Append(prefix).AppendFormat("if (!ReadFileDescriptor(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).AppendFormat("}\n");
}
void HdiFdTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeInt(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiFdTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readInt();\n", name.c_str(), parcelName.c_str());
}
void HdiFdTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readInt();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
void HdiFdTypeEmitter::EmitCWriteMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sWrite%s", methodPrefix.c_str(), typeName_.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *data, int fd);\n", methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *data, int fd)\n", methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("if (!HdfSbufWriteInt8(data, fd >= 0 ? 1 : 0)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to write fd vailed\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("if (!HdfSbufWriteFileDescriptor(data, fd)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to write fd\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("return true;\n");
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
void HdiFdTypeEmitter::EmitCReadMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sRead%s", methodPrefix.c_str(), typeName_.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *data, int *fd);\n", methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *data, int *fd)\n", methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("if (data == NULL) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: invalid HdfSBuf obj\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("if (fd == NULL) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: invalid fd pointer\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("*fd = -1;\n");
sb.Append(prefix + TAB).Append("bool fdValied = false;\n");
sb.Append(prefix + TAB).Append("if (!HdfSbufReadInt8(data, (int8_t*)&fdValied)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to read fdValied\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("if (!fdValied) {\n");
sb.Append(prefix + TAB + TAB).Append("return true;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("*fd = HdfSbufReadFileDescriptor(data);\n");
sb.Append(prefix + TAB).Append("if (*fd < 0) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to read fd\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("return true;\n");
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
void HdiFdTypeEmitter::EmitCStubReadMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
EmitCReadMethods(methods, prefix, methodPrefix, isDecl);
}
void HdiFdTypeEmitter::EmitCppWriteMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sWrite%s", methodPrefix.c_str(), typeName_.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static bool %s(MessageParcel &data, int fd);\n", methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static bool %s(MessageParcel &data, int fd)\n", methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("if (!data.WriteBool(fd >= 0 ? true : false)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to write fd vailed\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("if (fd < 0) {\n");
sb.Append(prefix + TAB + TAB).Append("return true;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("if (!data.WriteFileDescriptor(fd)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to write fd\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("return true;\n");
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
void HdiFdTypeEmitter::EmitCppReadMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sRead%s", methodPrefix.c_str(), typeName_.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static bool %s(MessageParcel &data, int &fd);\n", methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static bool %s(MessageParcel &data, int &fd)\n", methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("fd = -1;\n");
sb.Append(prefix + TAB).Append("bool fdValid = false;\n");
sb.Append(prefix + TAB).Append("if (!data.ReadBool(fdValid)) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to read fdValid\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).Append("if (fdValid) {\n");
sb.Append(prefix + TAB + TAB).Append("fd = data.ReadFileDescriptor();\n");
sb.Append(prefix + TAB + TAB).Append("if (fd < 0) {\n");
sb.Append(prefix + TAB + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to read fd\", __func__);\n");
sb.Append(prefix + TAB + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("return true;\n");
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,86 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_FDTYPE_EMITTER_H
#define OHOS_IDL_HDI_FDTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiFdTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCppWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCppReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_FDTYPE_EMITTER_H

View File

@ -0,0 +1,174 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_float_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiFloatTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_FLOAT;
}
std::string HdiFloatTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "float";
case TypeMode::PARAM_IN:
return "float";
case TypeMode::PARAM_OUT:
return "float*";
case TypeMode::LOCAL_VAR:
return "float";
default:
return "unknow type";
}
}
std::string HdiFloatTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "float";
case TypeMode::PARAM_IN:
case TypeMode::PARAM_INOUT:
return "float";
case TypeMode::PARAM_OUT:
return "float&";
case TypeMode::LOCAL_VAR:
return "float";
default:
return "unknow type";
}
}
std::string HdiFloatTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Float" : "float";
}
void HdiFloatTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteFloat(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadFloat(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadFloat(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteFloat(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadFloat(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteFloat(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadFloat(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteFloat(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadFloat(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiFloatTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeFloat(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiFloatTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readFloat();\n", name.c_str(), parcelName.c_str());
}
void HdiFloatTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readFloat();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_FLOATTYPE_EMITTER_H
#define OHOS_IDL_HDI_FLOATTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiFloatTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_FLOATTYPE_EMITTER_H

View File

@ -0,0 +1,174 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_int_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiIntTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_INT;
}
std::string HdiIntTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int32_t";
case TypeMode::PARAM_IN:
return "int32_t";
case TypeMode::PARAM_OUT:
return "int32_t*";
case TypeMode::LOCAL_VAR:
return "int32_t";
default:
return "unknow type";
}
}
std::string HdiIntTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int32_t";
case TypeMode::PARAM_IN:
case TypeMode::PARAM_INOUT:
return "int32_t";
case TypeMode::PARAM_OUT:
return "int32_t&";
case TypeMode::LOCAL_VAR:
return "int32_t";
default:
return "unknow type";
}
}
std::string HdiIntTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Integer" : "int";
}
void HdiIntTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt32(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt32(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt32(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt32(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt32(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiIntTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeInt(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiIntTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readInt();\n", name.c_str(), parcelName.c_str());
}
void HdiIntTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readInt();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_INTTYPE_EMITTER_H
#define OHOS_IDL_HDI_INTTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiIntTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_INTTYPE_EMITTER_H

View File

@ -0,0 +1,243 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_interface_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiInterfaceTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_INTERFACE;
}
std::string HdiInterfaceTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("struct %s", name_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("struct %s*", name_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("struct %s**", name_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("struct %s*", name_.c_str());
default:
return "unknow type";
}
}
std::string HdiInterfaceTypeEmitter::EmitCppType(TypeMode mode) const
{
std::string pointerName = "sptr";
if (Options::GetInstance().GetSystemLevel() == SystemLevel::LITE) {
pointerName = "std::shared_ptr";
}
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("%s<%s>", pointerName.c_str(), typeName_.c_str());
case TypeMode::PARAM_IN:
case TypeMode::PARAM_INOUT:
return StringHelper::Format(
"const %s<%s>&", pointerName.c_str(), typeName_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format(
"%s<%s>&", pointerName.c_str(), typeName_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("%s<%s>", pointerName.c_str(), typeName_.c_str());
default:
return "unknow type";
}
}
std::string HdiInterfaceTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return name_;
}
std::string HdiInterfaceTypeEmitter::EmitDescMacroName() const
{
return StringHelper::Format("%s_INTERFACE_DESC", StringHelper::StrToUpper(name_).c_str());
}
void HdiInterfaceTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!WriteInterface(%s, %s, %s)) {\n", parcelName.c_str(),
EmitDescMacroName().c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiInterfaceTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("*%s = Read%s(%s);\n", name.c_str(), name_.c_str(), replyParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (*%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiInterfaceTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = Read%s(%s);\n", name.c_str(), name_.c_str(), dataParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiInterfaceTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB)
.AppendFormat("HDF_LOGE(\"%%{public}s: parameter %s is nullptr!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
sb.Append("\n");
sb.Append(prefix).AppendFormat("if (!%s.WriteRemoteObject(", parcelName.c_str());
sb.AppendFormat("OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(%s, %s::GetDescriptor()))) {\n",
name.c_str(), typeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
std::string HdiInterfaceTypeEmitter::GetNamespaceWithColon() const
{
if (typeName_.empty()) {
return "";
}
size_t index = typeName_.rfind(':');
return (index == std::string::npos) ? typeName_.substr(0) : typeName_.substr(0, index + 1);
}
void HdiInterfaceTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string interfaceNamespace = GetNamespaceWithColon();
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("sptr<%s> %s;\n", typeName_.c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("sptr<IRemoteObject> %sRemote = %s.ReadRemoteObject();\n", name.c_str(),
parcelName.c_str());
sb.Append(prefix).AppendFormat("if (%sRemote == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: read an invalid remote object\", __func__);\n");
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("%s = new %s(%sRemote);\n", name.c_str(), (interfaceNamespace +
(StringHelper::StartWith(name_, "I") ? name_.substr(1) : name_) + "Proxy").c_str(), name.c_str());
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: failed to create interface object\", __func__);\n");
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiInterfaceTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeRemoteObject(%s.asObject());\n", parcelName.c_str(), name.c_str());
}
void HdiInterfaceTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
std::string stubName = StringHelper::StartWith(name_, "I") ? (name_.substr(1) + "Stub") : (name_ + "Stub");
sb.Append(prefix).AppendFormat(
"%s = %s.asInterface(%s.readRemoteObject());\n", name.c_str(), stubName.c_str(), parcelName.c_str());
}
void HdiInterfaceTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
std::string stubName = StringHelper::StartWith(name_, "I") ? (name_.substr(1) + "Stub") : (name_ + "Stub");
sb.Append(prefix).AppendFormat("%s %s = %s.asInterface(%s.readRemoteObject());\n",
EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), stubName.c_str(), parcelName.c_str());
}
void HdiInterfaceTypeEmitter::EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sWriteInterface", methodPrefix.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *parcel, const char *desc, void *interface);\n",
methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static bool %s(struct HdfSBuf *parcel, const char *desc, void *interface)\n",
methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("if (interface == NULL) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: invalid interface object\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).Append(
"struct HdfRemoteService **stub = StubCollectorGetOrNewObject(desc, interface);\n");
sb.Append(prefix + TAB).Append("if (stub == NULL) {\n");
sb.Append(prefix + TAB + TAB).Append(
"HDF_LOGE(\"%{public}s: failed to get stub of '%{public}s'\", __func__, desc);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).Append("if (HdfSbufWriteRemoteService(parcel, *stub) != HDF_SUCCESS) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: failed to write remote service\", __func__);\n");
sb.Append(prefix + TAB + TAB).Append("return false;\n");
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).Append("return true;\n");
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
void HdiInterfaceTypeEmitter::EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
StringBuilder sb;
std::string methodName = StringHelper::Format("%sRead%s", methodPrefix.c_str(), name_.c_str());
if (isDecl) {
sb.Append(prefix).AppendFormat("static struct %s *%s(struct HdfSBuf *parcel);\n", name_.c_str(),
methodName.c_str());
} else {
sb.Append("\n");
sb.Append(prefix).AppendFormat("static struct %s *%s(struct HdfSBuf *parcel)\n", name_.c_str(),
methodName.c_str());
sb.Append(prefix).Append("{\n");
sb.Append(prefix + TAB).Append("struct HdfRemoteService *remote = HdfSbufReadRemoteService(parcel);\n");
sb.Append(prefix + TAB).Append("if (remote == NULL) {\n");
sb.Append(prefix + TAB + TAB).Append("HDF_LOGE(\"%{public}s: ");
sb.AppendFormat(" failed to read remote service of '%s'\", __func__);\n", name_.c_str());
sb.Append(prefix + TAB + TAB).Append("return NULL;\n");
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).AppendFormat("return %sGet(remote);\n", name_.c_str());
sb.Append(prefix).Append("}\n");
}
methods.emplace(methodName, sb.ToString());
}
void HdiInterfaceTypeEmitter::EmitCStubReadMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const
{
EmitCReadMethods(methods, prefix, methodPrefix, isDecl);
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_INTERFACETYPE_EMITTER_H
#define OHOS_IDL_HDI_INTERFACETYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiInterfaceTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
std::string EmitDescMacroName() const;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCReadMethods(
UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const override;
void EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
private:
std::string GetNamespaceWithColon() const;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_INTERFACETYPE_EMITTER_H

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_long_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiLongTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_LONG;
}
std::string HdiLongTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int64_t";
case TypeMode::PARAM_IN:
return "int64_t";
case TypeMode::PARAM_OUT:
return "int64_t*";
case TypeMode::LOCAL_VAR:
return "int64_t";
default:
return "unknow type";
}
}
std::string HdiLongTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int64_t";
case TypeMode::PARAM_IN:
return "int64_t";
case TypeMode::PARAM_OUT:
return "int64_t&";
case TypeMode::LOCAL_VAR:
return "int64_t";
default:
return "unknow type";
}
}
std::string HdiLongTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Long" : "long";
}
void HdiLongTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt64(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt64(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt64(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt64(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt64(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiLongTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeLong(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiLongTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readLong();\n", name.c_str(), parcelName.c_str());
}
void HdiLongTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readLong();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_LONGTYPE_EMITTER_H
#define OHOS_IDL_HDI_LONGTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiLongTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_LONGTYPE_EMITTER_H

View File

@ -0,0 +1,210 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_map_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiMapTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_MAP;
}
std::string HdiMapTypeEmitter::EmitCType(TypeMode mode) const
{
// c language has no map type
return "/";
}
std::string HdiMapTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format(
"std::map<%s, %s>", keyEmitter_->EmitCppType().c_str(), valueEmitter_->EmitCppType().c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format(
"const std::map<%s, %s>&", keyEmitter_->EmitCppType().c_str(), valueEmitter_->EmitCppType().c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format(
"std::map<%s, %s>&", keyEmitter_->EmitCppType().c_str(), valueEmitter_->EmitCppType().c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format(
"std::map<%s, %s>", keyEmitter_->EmitCppType().c_str(), valueEmitter_->EmitCppType().c_str());
default:
return "unknow type";
}
}
std::string HdiMapTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return StringHelper::Format("HashMap<%s, %s>", keyEmitter_->EmitJavaType(mode, true).c_str(),
valueEmitter_->EmitJavaType(mode, true).c_str());
}
void HdiMapTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint32(%s.size())) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
std::string elementName = StringHelper::Format("it%d", innerLevel++);
sb.Append(prefix).AppendFormat("for (auto %s : %s) {\n", elementName.c_str(), name.c_str());
std::string keyName = StringHelper::Format("(%s.first)", elementName.c_str());
std::string valueName = StringHelper::Format("(%s.second)", elementName.c_str());
keyEmitter_->EmitCppWriteVar(parcelName, keyName, sb, prefix + TAB, innerLevel);
valueEmitter_->EmitCppWriteVar(parcelName, valueName, sb, prefix + TAB, innerLevel);
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("uint32_t %sSize = 0;\n", name.c_str());
sb.Append(prefix).AppendFormat("if (!%s.ReadUint32(%sSize)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: failed to read size\", __func__);\n");
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("for (uint32_t i = 0; i < %sSize; ++i) {\n", name.c_str());
std::string keyName = StringHelper::Format("key%d", innerLevel);
std::string valueName = StringHelper::Format("value%d", innerLevel);
innerLevel++;
keyEmitter_->EmitCppReadVar(keyName, sb, prefix + TAB, mode, innerLevel);
valueEmitter_->EmitCppReadVar(valueName, sb, prefix + TAB, mode, innerLevel);
sb.Append(prefix + TAB).AppendFormat("%s[%s] = %s;\n", name.c_str(), keyName.c_str(), valueName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint32(%s.size())) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s.size failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
std::string elementName = StringHelper::Format("it%d", innerLevel++);
sb.Append(prefix).AppendFormat("for (const auto& %s : %s) {\n", elementName.c_str(), name.c_str());
std::string keyName = StringHelper::Format("(%s.first)", elementName.c_str());
std::string valName = StringHelper::Format("(%s.second)", elementName.c_str());
keyEmitter_->EmitCppMarshalling(parcelName, keyName, sb, prefix + TAB, innerLevel);
valueEmitter_->EmitCppMarshalling(parcelName, valName, sb, prefix + TAB, innerLevel);
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
size_t index = name.find('.', 0);
std::string memberName = (index == std::string::npos) ? name : StringHelper::SubStr(name, index + 1);
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s;\n", EmitCppType().c_str(), memberName.c_str());
}
sb.Append(prefix).AppendFormat("uint32_t %sSize = 0;\n", memberName.c_str());
sb.Append(prefix).AppendFormat("if (!%s.ReadUint32(%sSize)) {\n", parcelName.c_str(), memberName.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: failed to read size\", __func__);\n");
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("for (uint32_t i = 0; i < %sSize; ++i) {\n", memberName.c_str());
std::string keyName = StringHelper::Format("key%d", innerLevel);
std::string valueName = StringHelper::Format("value%d", innerLevel);
innerLevel++;
keyEmitter_->EmitCppUnMarshalling(parcelName, keyName, sb, prefix + TAB, innerLevel);
valueEmitter_->EmitCppUnMarshalling(parcelName, valueName, sb, prefix + TAB, innerLevel);
sb.Append(prefix + TAB).AppendFormat("%s[%s] = %s;\n", name.c_str(), keyName.c_str(), valueName.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeInt(%s.size());\n", parcelName.c_str(), name.c_str());
sb.Append(prefix).AppendFormat("for (Map.Entry<%s, %s> entry : %s.entrySet()) {\n",
keyEmitter_->EmitJavaType(TypeMode::NO_MODE, true).c_str(),
valueEmitter_->EmitJavaType(TypeMode::NO_MODE, true).c_str(), name.c_str());
keyEmitter_->EmitJavaWriteVar(parcelName, "entry.getKey()", sb, prefix + TAB);
valueEmitter_->EmitJavaWriteVar(parcelName, "entry.getValue()", sb, prefix + TAB);
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("int %sSize = %s.readInt();\n", name.c_str(), parcelName.c_str());
sb.Append(prefix).AppendFormat("for (int i = 0; i < %sSize; ++i) {\n", name.c_str());
keyEmitter_->EmitJavaReadInnerVar(parcelName, "key", false, sb, prefix + TAB);
valueEmitter_->EmitJavaReadInnerVar(parcelName, "value", false, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("%s.put(key, value);\n", name.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s %s = new Hash%s();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(),
EmitJavaType(TypeMode::NO_MODE).c_str());
sb.Append(prefix).AppendFormat("int %sSize = %s.readInt();\n", name.c_str(), parcelName.c_str());
sb.Append(prefix).AppendFormat("for (int i = 0; i < %sSize; ++i) {\n", name.c_str());
keyEmitter_->EmitJavaReadInnerVar(parcelName, "key", true, sb, prefix + TAB);
valueEmitter_->EmitJavaReadInnerVar(parcelName, "value", true, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("%s.put(key, value);\n", name.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiMapTypeEmitter::EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
keyEmitter_->EmitCWriteMethods(methods, prefix, methodPrefix, isDecl);
valueEmitter_->EmitCWriteMethods(methods, prefix, methodPrefix, isDecl);
}
void HdiMapTypeEmitter::EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
keyEmitter_->EmitCReadMethods(methods, prefix, methodPrefix, isDecl);
valueEmitter_->EmitCReadMethods(methods, prefix, methodPrefix, isDecl);
}
void HdiMapTypeEmitter::EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
keyEmitter_->EmitCStubReadMethods(methods, prefix, methodPrefix, isDecl);
valueEmitter_->EmitCStubReadMethods(methods, prefix, methodPrefix, isDecl);
}
void HdiMapTypeEmitter::EmitCppReadMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
keyEmitter_->EmitCppReadMethods(methods, prefix, methodPrefix, isDecl);
valueEmitter_->EmitCppReadMethods(methods, prefix, methodPrefix, isDecl);
}
void HdiMapTypeEmitter::EmitCppWriteMethods(UtilMethodMap &methods, const std::string &prefix,
const std::string &methodPrefix, bool isDecl) const
{
keyEmitter_->EmitCppWriteMethods(methods, prefix, methodPrefix, isDecl);
valueEmitter_->EmitCppWriteMethods(methods, prefix, methodPrefix, isDecl);
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_MAPTYPE_EMITTER_H
#define OHOS_IDL_HDI_MAPTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiMapTypeEmitter : public HdiTypeEmitter {
public:
inline void SetKeyEmitter(const AutoPtr<HdiTypeEmitter> &keyEmitter)
{
keyEmitter_ = keyEmitter;
}
inline void SetValueEmitter(const AutoPtr<HdiTypeEmitter> &valueEmitter)
{
valueEmitter_ = valueEmitter;
}
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCppWriteMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
void EmitCppReadMethods(UtilMethodMap &methods, const std::string &prefix, const std::string &methodPrefix,
bool isDecl) const override;
private:
AutoPtr<HdiTypeEmitter> keyEmitter_;
AutoPtr<HdiTypeEmitter> valueEmitter_;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_MAPTYPE_EMITTER_H

View File

@ -0,0 +1,162 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_native_buffer_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiNativeBufferTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_NATIVE_BUFFER;
}
std::string HdiNativeBufferTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "BufferHandle*";
case TypeMode::PARAM_IN:
return "const BufferHandle*";
case TypeMode::PARAM_OUT:
return "BufferHandle**";
case TypeMode::LOCAL_VAR:
return "BufferHandle*";
default:
return "unknow type";
}
}
std::string HdiNativeBufferTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "sptr<NativeBuffer>";
case TypeMode::PARAM_IN:
return "const sptr<NativeBuffer>&";
case TypeMode::PARAM_OUT:
return "sptr<NativeBuffer>&";
case TypeMode::LOCAL_VAR:
return "sptr<NativeBuffer>";
default:
return "unknow type";
}
}
void HdiNativeBufferTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteNativeBufferHandle(%s, %s)) {\n", parcelName.c_str(),
name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to write %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix);
if (!isInnerType) {
sb.Append("*");
}
sb.AppendFormat("%s = HdfSbufReadNativeBufferHandle(%s);\n", name.c_str(), replyParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = HdfSbufReadNativeBufferHandle(%s);\n", name.c_str(), dataParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteStrongParcelable(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to write %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
sb.Append(prefix);
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.AppendFormat("%s ", EmitCppType().c_str());
}
sb.AppendFormat("%s = %s.ReadStrongParcelable<NativeBuffer>();\n", name.c_str(), parcelName.c_str());
}
void HdiNativeBufferTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteNativeBufferHandle(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("%s = HdfSbufReadNativeBufferHandle(data);\n", name.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteStrongParcelable(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to write %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiNativeBufferTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix);
if (innerLevel > 0) {
sb.AppendFormat("%s ", EmitCppType().c_str());
}
sb.AppendFormat("%s = %s.ReadStrongParcelable<NativeBuffer>();\n", name.c_str(), parcelName.c_str());
}
void HdiNativeBufferTypeEmitter::EmitMemoryRecycle(
const std::string &name, bool ownership, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (%s != NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("FreeNativeBufferHandle(%s);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = NULL;\n", name.c_str());
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_NATIVE_BUFFER_TYPE_EMITTER_H
#define OHOS_IDL_HDI_NATIVE_BUFFER_TYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiNativeBufferTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_NATIVE_BUFFER_TYPE_EMITTER_H

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_pointer_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiPointerTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_POINTER;
}
std::string HdiPointerTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "void*";
case TypeMode::PARAM_IN:
return "const void*";
case TypeMode::PARAM_OUT:
return "void**";
case TypeMode::LOCAL_VAR:
return "void*";
default:
return "unknow type";
}
}
std::string HdiPointerTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "void*";
case TypeMode::PARAM_IN:
return "const void*";
case TypeMode::PARAM_OUT:
return "void*&";
case TypeMode::LOCAL_VAR:
return "void*";
default:
return "unknow type";
}
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_POINTERTYPE_EMITTER_H
#define OHOS_IDL_HDI_POINTERTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiPointerTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
// the 'Pointer' type only support use in 'passthrough' mode
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_POINTERTYPE_EMITTER_H

View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_seq_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiSeqTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_SEQUENCEABLE;
}
std::string HdiSeqTypeEmitter::EmitCType(TypeMode mode) const
{
// c language has no Sequenceable type
return "/";
}
std::string HdiSeqTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("sptr<%s>", typeName_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("const sptr<%s>&", typeName_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("sptr<%s>&", typeName_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("sptr<%s>", typeName_.c_str());
default:
return "unknow type";
}
}
std::string HdiSeqTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return name_;
}
void HdiSeqTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteStrongParcelable(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiSeqTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("sptr<%s> %s = %s.ReadStrongParcelable<%s>();\n", name_.c_str(), name.c_str(),
parcelName.c_str(), name_.c_str());
} else {
sb.Append(prefix).AppendFormat(
"%s = %s.ReadStrongParcelable<%s>();\n", name.c_str(), parcelName.c_str(), name_.c_str());
}
}
void HdiSeqTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteStrongParcelable(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiSeqTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = %s.ReadStrongParcelable<%s>();\n", EmitCppType().c_str(), name.c_str(),
parcelName.c_str(), name_.c_str());
} else {
sb.Append(prefix).AppendFormat(
"%s = %s.ReadStrongParcelable<%s>();\n", name.c_str(), parcelName.c_str(), name_.c_str());
}
}
void HdiSeqTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
if (EmitJavaType(TypeMode::NO_MODE) == "IRemoteObject") {
sb.Append(prefix).AppendFormat("%s.writeRemoteObject(%s);\n", parcelName.c_str(), name.c_str());
return;
}
sb.Append(prefix).AppendFormat("%s.writeSequenceable(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiSeqTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
if (EmitJavaType(TypeMode::NO_MODE) == "IRemoteObject") {
sb.Append(prefix).AppendFormat("%s = %s.readRemoteObject();\n", name.c_str(), parcelName.c_str());
return;
}
sb.Append(prefix).AppendFormat("%s.readSequenceable(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiSeqTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
if (!isInner && EmitJavaType(TypeMode::NO_MODE) == "IRemoteObject") {
sb.Append(prefix).AppendFormat("IRemoteObject %s = %s.readRemoteObject();\n", name.c_str(), parcelName.c_str());
return;
}
if (!isInner) {
sb.Append(prefix).AppendFormat("%s %s = new %s();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(),
EmitJavaType(TypeMode::NO_MODE).c_str());
}
sb.Append(prefix).AppendFormat("%s.readSequenceable(%s);\n", parcelName.c_str(), name.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_SEQTYPE_EMITTER_H
#define OHOS_IDL_HDI_SEQTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiSeqTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_SEQTYPE_EMITTER_H

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_short_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiShortTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_SHORT;
}
std::string HdiShortTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int16_t";
case TypeMode::PARAM_IN:
return "int16_t";
case TypeMode::PARAM_OUT:
return "int16_t*";
case TypeMode::LOCAL_VAR:
return "int16_t";
default:
return "unknow type";
}
}
std::string HdiShortTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "int16_t";
case TypeMode::PARAM_IN:
return "int16_t";
case TypeMode::PARAM_OUT:
return "int16_t&";
case TypeMode::LOCAL_VAR:
return "int16_t";
default:
return "unknow type";
}
}
std::string HdiShortTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return isInnerType ? "Short" : "short";
}
void HdiShortTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt16(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt16(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt16(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteInt16(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadInt16(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteInt16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadInt16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiShortTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeShort(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiShortTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readShort();\n", name.c_str(), parcelName.c_str());
}
void HdiShortTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readShort();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_SHORTTYPE_EMITTER_H
#define OHOS_IDL_HDI_SHORTTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiShortTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_SHORTTYPE_EMITTER_H

View File

@ -0,0 +1,191 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_smq_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiSmqTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_SMQ;
}
std::string HdiSmqTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("std::shared_ptr<SharedMemQueue<%s>>",
innerTypeEmitter_->EmitCppType().c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format(
"const std::shared_ptr<SharedMemQueue<%s>>&", innerTypeEmitter_->EmitCppType().c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("std::shared_ptr<SharedMemQueue<%s>>&",
innerTypeEmitter_->EmitCppType().c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("std::shared_ptr<SharedMemQueue<%s>>",
innerTypeEmitter_->EmitCppType().c_str());
default:
return "unknow type";
}
}
void HdiSmqTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (%s == nullptr || !%s->IsGood() || %s->GetMeta() == nullptr || ", name.c_str(), name.c_str(), name.c_str());
sb.AppendFormat("!%s->GetMeta()->Marshalling(%s)) {\n", name.c_str(), parcelName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiSmqTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
std::string metaVarName = StringHelper::Format("%sMeta_", name.c_str());
sb.Append(prefix).AppendFormat(
"std::shared_ptr<SharedMemQueueMeta<%s>> %s = ", innerTypeEmitter_->EmitCppType().c_str(), metaVarName.c_str());
sb.AppendFormat("SharedMemQueueMeta<%s>::UnMarshalling(%s);\n", innerTypeEmitter_->EmitCppType().c_str(),
parcelName.c_str());
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", metaVarName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: SharedMemQueueMeta is nullptr\", __func__);\n");
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n\n");
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = ", EmitCppType(TypeMode::LOCAL_VAR).c_str(), name.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = ", name.c_str());
}
sb.AppendFormat(
"std::make_shared<SharedMemQueue<%s>>(*%s);\n", innerTypeEmitter_->EmitCppType().c_str(), metaVarName.c_str());
}
void HdiSmqTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (%s == nullptr || !%s->IsGood() || %s->GetMeta() == nullptr || ", name.c_str(), name.c_str(), name.c_str());
sb.AppendFormat("!%s->GetMeta()->Marshalling(%s)) {\n", name.c_str(), parcelName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiSmqTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
size_t index = name.find('.');
std::string memberName = (index == std::string::npos) ? name : StringHelper::SubStr(name, index + 1);
std::string metaVarName = StringHelper::Format("%sMeta_", memberName.c_str());
sb.Append(prefix).AppendFormat(
"std::shared_ptr<SharedMemQueueMeta<%s>> %s = ", innerTypeEmitter_->EmitCppType().c_str(), metaVarName.c_str());
sb.AppendFormat(
"SharedMemQueueMeta<%s>::UnMarshalling(%s);\n", innerTypeEmitter_->EmitCppType().c_str(), parcelName.c_str());
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", metaVarName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: SharedMemQueueMeta is nullptr\", __func__);\n");
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n\n");
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = ", EmitCppType(TypeMode::LOCAL_VAR).c_str(), memberName.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = ", name.c_str());
}
sb.AppendFormat(
"std::make_shared<SharedMemQueue<%s>>(*%s);\n", innerTypeEmitter_->EmitCppType().c_str(), metaVarName.c_str());
}
TypeKind HdiAshmemTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_ASHMEM;
}
std::string HdiAshmemTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("sptr<Ashmem>");
case TypeMode::PARAM_IN:
return StringHelper::Format("const sptr<Ashmem>&");
case TypeMode::PARAM_OUT:
return StringHelper::Format("sptr<Ashmem>&");
case TypeMode::LOCAL_VAR:
return StringHelper::Format("sptr<Ashmem>");
default:
return "unknow type";
}
}
void HdiAshmemTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (%s == nullptr || !%s.WriteAshmem(%s)) {\n", name.c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to write %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiAshmemTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat(
"%s %s = %s.ReadAshmem();\n", EmitCppType().c_str(), name.c_str(), parcelName.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = %s.ReadAshmem();\n", name.c_str(), parcelName.c_str());
}
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: failed to read ashmem object\", __func__);\n");
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiAshmemTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (%s == nullptr || !%s.WriteAshmem(%s)) {\n", name.c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to write %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiAshmemTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat(
"%s %s = %s.ReadAshmem();\n", EmitCppType().c_str(), name.c_str(), parcelName.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = %s.ReadAshmem();\n", name.c_str(), parcelName.c_str());
}
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).Append("HDF_LOGE(\"%{public}s: failed to read ashmem object\", __func__);\n");
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_SMQTYPE_EMITTER_H
#define OHOS_IDL_HDI_SMQTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiSmqTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
inline void SetInnerTypeEmitter(const AutoPtr<HdiTypeEmitter> &typeEmitter)
{
innerTypeEmitter_ = typeEmitter;
}
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
private:
AutoPtr<HdiTypeEmitter> innerTypeEmitter_;
};
class HdiAshmemTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_SMQTYPE_EMITTER_H

View File

@ -0,0 +1,243 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_string_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiStringTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_STRING;
}
std::string HdiStringTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "char*";
case TypeMode::PARAM_IN:
return "const char*";
case TypeMode::PARAM_OUT:
return "char*";
case TypeMode::LOCAL_VAR:
return "char*";
default:
return "unknow type";
}
}
std::string HdiStringTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "std::string";
case TypeMode::PARAM_IN:
return "const std::string&";
case TypeMode::PARAM_OUT:
return "std::string&";
case TypeMode::LOCAL_VAR:
return "std::string";
default:
return "unknow type";
}
}
std::string HdiStringTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
return "std::string";
}
void HdiStringTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteString(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCProxyWriteOutVar(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
std::string lenName = StringHelper::Format("%sLen", name.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL || %s == 0) {\n", name.c_str(), lenName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: %s is invalid\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint32(%s, %s)) {\n", dataParcelName_.c_str(), lenName.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", lenName.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n\n");
}
void HdiStringTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("const char *%s = HdfSbufReadString(%s);\n", name.c_str(), replyParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("const char *%s = HdfSbufReadString(%s);\n",
name.c_str(), dataParcelName_.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCStubReadOutVar(const std::string &memFlagName, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string lenName = StringHelper::Format("%sLen", name.c_str());
sb.Append(prefix).AppendFormat("if (%s) {\n", memFlagName.c_str());
sb.Append(prefix + TAB).AppendFormat("if (!HdfSbufReadUint32(%s, &%s)) {\n", dataParcelName_.c_str(),
lenName.c_str());
sb.Append(prefix + TAB + TAB)
.AppendFormat("HDF_LOGE(\"%%{public}s: read %s size failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n\n");
sb.Append(prefix + TAB).AppendFormat("%s(%s, >, %s / sizeof(char), %s, HDF_ERR_INVALID_PARAM, %s);\n",
CHECK_VALUE_RET_GOTO_MACRO, lenName.c_str(), MAX_BUFF_SIZE_MACRO, errorCodeName_.c_str(), gotoLabel.c_str());
sb.Append(prefix + TAB).AppendFormat("if (%s > 0) {\n", lenName.c_str());
sb.Append(prefix + TAB + TAB)
.AppendFormat("%s = (%s)OsalMemCalloc(%s);\n", name.c_str(), EmitCType().c_str(), lenName.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB + TAB + TAB)
.AppendFormat("HDF_LOGE(\"%%{public}s: malloc %s failed\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB + TAB).AppendFormat("%s = HDF_ERR_MALLOC_FAIL;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB + TAB).Append("}\n");
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix).Append("} else {\n");
sb.Append(prefix + TAB)
.AppendFormat("%s = (%s)OsalMemCalloc(%s);\n", name.c_str(), EmitCType().c_str(), MAX_BUFF_SIZE_MACRO);
sb.Append(prefix + TAB).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB + TAB)
.AppendFormat("HDF_LOGE(\"%%{public}s: malloc %s failed\", __func__);\n", name.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("%s = HDF_ERR_MALLOC_FAIL;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix + TAB).Append("}\n");
sb.Append(prefix + TAB).AppendFormat("%sLen = %s;\n", name.c_str(), MAX_BUFF_SIZE_MACRO);
sb.Append(prefix).Append("}\n\n");
}
void HdiStringTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteCString(%s.c_str())) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("const char* %sCp = %s.ReadCString();\n", name.c_str(), parcelName.c_str());
sb.Append(prefix).AppendFormat("if (%sCp == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = %sCp;\n", EmitCppType().c_str(), name.c_str(), name.c_str());
} else {
sb.Append(prefix).AppendFormat("%s = %sCp;\n", name.c_str(), name.c_str());
}
}
void HdiStringTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteString(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("const char *%s = HdfSbufReadString(data);\n", name.c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteCString(%s.c_str())) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("const char* %s = %s.ReadCString();\n", name.c_str(), parcelName.c_str());
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitMemoryRecycle(
const std::string &name, bool ownership, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (%s != NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("OsalMemFree(%s);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = NULL;\n", name.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStringTypeEmitter::EmitJavaWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, TypeMode mode) const
{
sb.Append(prefix).AppendFormat("%s.writeString(%s);\n", parcelName.c_str(), name.c_str());
}
void HdiStringTypeEmitter::EmitJavaReadVar(
const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("%s = %s.readString();\n", name.c_str(), parcelName.c_str());
}
void HdiStringTypeEmitter::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"%s %s = %s.readString();\n", EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str(), parcelName.c_str());
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_STRINGTYPE_EMITTER_H
#define OHOS_IDL_HDI_STRINGTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiStringTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyWriteOutVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadOutVar(const std::string &memFlagName, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const override;
void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix) const override;
void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb,
const std::string &prefix) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_STRINGTYPE_EMITTER_H

View File

@ -0,0 +1,213 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_struct_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiStructTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_STRUCT;
}
std::string HdiStructTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("struct %s", name_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("const struct %s*", name_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("struct %s*", name_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("struct %s*", name_.c_str());
default:
return "unknow type";
}
}
std::string HdiStructTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("%s", typeName_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("const %s&", typeName_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("%s&", typeName_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("%s", typeName_.c_str());
default:
return "unknow type";
}
}
std::string HdiStructTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// currently, Java does not support the struct type.
return "/";
}
std::string HdiStructTypeEmitter::EmitCTypeDecl() const
{
StringBuilder sb;
sb.AppendFormat("struct %s {\n", name_.c_str());
for (auto it : members_) {
AutoPtr<HdiTypeEmitter> member = std::get<1>(it);
std::string memberName = std::get<0>(it);
sb.Append(TAB).AppendFormat("%s %s;\n", member->EmitCType().c_str(), memberName.c_str());
if (member->GetTypeKind() == TypeKind::TYPE_ARRAY || member->GetTypeKind() == TypeKind::TYPE_LIST) {
sb.Append(TAB).AppendFormat("uint32_t %sLen;\n", memberName.c_str());
}
}
sb.Append("}");
if (IsPod()) {
sb.Append(" __attribute__ ((aligned(8)))");
}
sb.Append(";");
return sb.ToString();
}
std::string HdiStructTypeEmitter::EmitCppTypeDecl() const
{
StringBuilder sb;
sb.AppendFormat("struct %s {\n", name_.c_str());
for (auto it : members_) {
AutoPtr<HdiTypeEmitter> member = std::get<1>(it);
std::string memberName = std::get<0>(it);
sb.Append(TAB).AppendFormat("%s %s;\n", member->EmitCppType().c_str(), memberName.c_str());
}
sb.Append("}");
if (IsPod()) {
sb.Append(" __attribute__ ((aligned(8)))");
}
sb.Append(";");
return sb.ToString();
}
void HdiStructTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat(
"if (!%sBlockMarshalling(%s, %s)) {\n", name_.c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"if (!%sBlockUnmarshalling(%s, %s)) {\n", name_.c_str(), replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat(
"if (!%sBlockUnmarshalling(%s, %s)) {\n", name_.c_str(), dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).AppendFormat("}\n");
}
void HdiStructTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (!%sBlockMarshalling(%s, %s)) {\n", EmitCppType().c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat(
"if (!%sBlockUnmarshalling(%s, %s)) {\n", name_.c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!%sBlockMarshalling(data, &%s)) {\n", name_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!%sBlockUnmarshalling(data, %s)) {\n", name_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat(
"if (!%sBlockMarshalling(%s, %s)) {\n", name_.c_str(), parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%sBlockUnmarshalling(data, %s)) {\n", EmitCppType().c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiStructTypeEmitter::EmitMemoryRecycle(
const std::string &name, bool ownership, StringBuilder &sb, const std::string &prefix) const
{
std::string varName = name;
if (ownership) {
sb.Append(prefix).AppendFormat("if (%s != NULL) {\n", varName.c_str());
sb.Append(prefix + TAB).AppendFormat("%sFree(%s, true);\n", name_.c_str(), varName.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = NULL;\n", varName.c_str());
sb.Append(prefix).Append("}\n");
} else {
sb.Append(prefix).AppendFormat("%sFree(&%s, false);\n", name_.c_str(), name.c_str());
}
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_STRUCTTYPE_EMITTER_H
#define OHOS_IDL_HDI_STRUCTTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiStructTypeEmitter : public HdiTypeEmitter {
public:
inline void AddMember(std::string name, const AutoPtr<HdiTypeEmitter> &memberEmiter)
{
members_.push_back(std::make_tuple(name, memberEmiter));
}
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
std::string EmitCTypeDecl() const override;
std::string EmitCppTypeDecl() const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb,
const std::string &prefix) const override;
private:
std::vector<std::tuple<std::string, AutoPtr<HdiTypeEmitter>>> members_;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_STRUCTTYPE_EMITTER_H

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_uchar_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiUcharTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_UCHAR;
}
std::string HdiUcharTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint8_t";
case TypeMode::PARAM_IN:
return "uint8_t";
case TypeMode::PARAM_OUT:
return "uint8_t*";
case TypeMode::LOCAL_VAR:
return "uint8_t";
default:
return "unknow type";
}
}
std::string HdiUcharTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint8_t";
case TypeMode::PARAM_IN:
return "uint8_t";
case TypeMode::PARAM_OUT:
return "uint8_t&";
case TypeMode::LOCAL_VAR:
return "uint8_t";
default:
return "unknow type";
}
}
std::string HdiUcharTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// unsupported type
return "/";
}
void HdiUcharTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint8(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint8(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint8(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint8(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint8(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUcharTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint8(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_UCHARTYPE_EMITTER_H
#define OHOS_IDL_HDI_UCHARTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiUcharTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_UCHARTYPE_EMITTER_H

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_uint_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiUintTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_UINT;
}
std::string HdiUintTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint32_t";
case TypeMode::PARAM_IN:
return "uint32_t";
case TypeMode::PARAM_OUT:
return "uint32_t*";
case TypeMode::LOCAL_VAR:
return "uint32_t";
default:
return "unknow type";
}
}
std::string HdiUintTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint32_t";
case TypeMode::PARAM_IN:
return "uint32_t";
case TypeMode::PARAM_OUT:
return "uint32_t&";
case TypeMode::LOCAL_VAR:
return "uint32_t";
default:
return "unknow type";
}
}
std::string HdiUintTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// unsupported type
return "/";
}
void HdiUintTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint32(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint32(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint32(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint32(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint32(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUintTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint32(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_UINTTYPE_EMITTER_H
#define OHOS_IDL_HDI_UINTTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiUintTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_UINTTYPE_EMITTER_H

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_ulong_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiUlongTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_ULONG;
}
std::string HdiUlongTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint64_t";
case TypeMode::PARAM_IN:
return "uint64_t";
case TypeMode::PARAM_OUT:
return "uint64_t*";
case TypeMode::LOCAL_VAR:
return "uint64_t";
default:
return "unknow type";
}
}
std::string HdiUlongTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint64_t";
case TypeMode::PARAM_IN:
return "uint64_t";
case TypeMode::PARAM_OUT:
return "uint64_t&";
case TypeMode::LOCAL_VAR:
return "uint64_t";
default:
return "unknow type";
}
}
std::string HdiUlongTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// unsupported type
return "/";
}
void HdiUlongTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint64(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint64(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint64(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint64(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint64(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUlongTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint64(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_ULONGTYPE_EMITTER_H
#define OHOS_IDL_HDI_ULONGTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiUlongTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_ASTULONGTYPE_H

View File

@ -0,0 +1,268 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_union_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiUnionTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_UNION;
}
std::string HdiUnionTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("union %s", name_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("const union %s*", name_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("union %s*", name_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("union %s*", name_.c_str());
default:
return "unknow type";
}
}
std::string HdiUnionTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return StringHelper::Format("%s", typeName_.c_str());
case TypeMode::PARAM_IN:
return StringHelper::Format("const %s&", typeName_.c_str());
case TypeMode::PARAM_OUT:
return StringHelper::Format("%s&", typeName_.c_str());
case TypeMode::LOCAL_VAR:
return StringHelper::Format("%s", typeName_.c_str());
default:
return "unknow type";
}
}
std::string HdiUnionTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// unsupported type
return "/";
}
std::string HdiUnionTypeEmitter::EmitCTypeDecl() const
{
StringBuilder sb;
sb.AppendFormat("union %s {\n", name_.c_str());
for (auto it : members_) {
AutoPtr<HdiTypeEmitter> member = std::get<1>(it);
std::string memberName = std::get<0>(it);
sb.Append(TAB).AppendFormat("%s %s;\n", member->EmitCType().c_str(), memberName.c_str());
if (member->GetTypeKind() == TypeKind::TYPE_ARRAY || member->GetTypeKind() == TypeKind::TYPE_LIST) {
sb.Append(TAB).AppendFormat("uint32_t %sLen;\n", memberName.c_str());
}
}
sb.Append("} __attribute__ ((aligned(8)));");
return sb.ToString();
}
std::string HdiUnionTypeEmitter::EmitCppTypeDecl() const
{
StringBuilder sb;
sb.AppendFormat("union %s {\n", name_.c_str());
for (auto it : members_) {
AutoPtr<HdiTypeEmitter> member = std::get<1>(it);
std::string memberName = std::get<0>(it);
sb.Append(TAB).AppendFormat("%s %s;\n", member->EmitCppType().c_str(), memberName.c_str());
}
sb.Append("} __attribute__ ((aligned(8)));");
return sb.ToString();
}
std::string HdiUnionTypeEmitter::EmitJavaTypeDecl() const
{
StringBuilder sb;
return sb.ToString();
}
void HdiUnionTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if (Options::GetInstance().DoGenerateKernelCode()) {
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteBuffer(%s, (const void *)%s, sizeof(%s))) {\n",
parcelName.c_str(), name.c_str(), EmitCType().c_str());
} else {
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUnpadBuffer(%s, (const uint8_t *)%s, sizeof(%s))) {\n",
parcelName.c_str(), name.c_str(), EmitCType().c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
if (Options::GetInstance().DoGenerateKernelCode()) {
sb.Append(prefix).AppendFormat("%s *%s = NULL;\n", EmitCType().c_str(), name.c_str());
sb.Append(prefix).Append("uint32_t len = 0;\n");
sb.Append(prefix).AppendFormat(
"if (!HdfSbufReadBuffer(%s, (const void **)&%s, &len)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("if (%s == NULL || sizeof(%s) != len) {\n", name.c_str(), EmitCType().c_str());
} else {
sb.Append(prefix).AppendFormat("const %s *%s = (%s *)HdfSbufReadUnpadBuffer(%s, sizeof(%s));\n",
EmitCType().c_str(), name.c_str(), EmitCType().c_str(), replyParcelName_.c_str(), EmitCType().c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
if (Options::GetInstance().DoGenerateKernelCode()) {
sb.Append(prefix).AppendFormat("%s *%s = NULL;\n", EmitCType().c_str(), name.c_str());
sb.Append(prefix).Append("uint32_t len = 0;\n");
sb.Append(prefix).AppendFormat(
"if (!HdfSbufReadBuffer(%s, (const void **)&%s, &len)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("if (%s == NULL || sizeof(%s) != len) {\n", name.c_str(), EmitCType().c_str());
} else {
sb.Append(prefix).AppendFormat("const %s *%s = (%s *)HdfSbufReadUnpadBuffer(%s, sizeof(%s));\n",
EmitCType().c_str(), name.c_str(), EmitCType().c_str(), dataParcelName_.c_str(), EmitCType().c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUnpadBuffer((const uint8_t *)&%s, sizeof(%s))) {\n",
parcelName.c_str(), name.c_str(), EmitCppType().c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("const %s *%sCp = reinterpret_cast<const %s *>(%s.ReadUnpadBuffer(sizeof(%s)));\n",
EmitCppType().c_str(), name.c_str(), EmitCppType().c_str(), parcelName.c_str(), EmitCppType().c_str());
sb.Append(prefix).AppendFormat("if (%sCp == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("if (memcpy_s(&%s, sizeof(%s), %sCp, sizeof(%s)) != EOK) {\n", name.c_str(),
EmitCppType().c_str(), name.c_str(), EmitCppType().c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: failed to memcpy %s\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
if (Options::GetInstance().DoGenerateKernelCode()) {
sb.Append(prefix).AppendFormat(
"if (!HdfSbufWriteBuffer(data, (const void *)&%s, sizeof(%s))) {\n", name.c_str(), EmitCType().c_str());
} else {
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUnpadBuffer(data, (const uint8_t *)&%s, sizeof(%s))) {\n",
name.c_str(), EmitCType().c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
if (Options::GetInstance().DoGenerateKernelCode()) {
sb.Append(prefix).AppendFormat("%s *%s = NULL;\n", EmitCType().c_str(), name.c_str());
sb.Append(prefix).Append("uint32_t len = 0;\n");
sb.Append(prefix).AppendFormat("if (!HdfSbufReadBuffer(data, (const void **)&%s, &len)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n\n");
sb.Append(prefix).AppendFormat("if (%s == NULL || sizeof(%s) != len) {\n", name.c_str(), EmitCType().c_str());
} else {
sb.Append(prefix).AppendFormat("const %s *%s = (const %s *)HdfSbufReadUnpadBuffer(data, sizeof(%s));\n",
EmitCType().c_str(), name.c_str(), EmitCType().c_str(), EmitCType().c_str());
sb.Append(prefix).AppendFormat("if (%s == NULL) {\n", name.c_str());
}
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUnpadBuffer((const void*)&%s, sizeof(%s))) {\n", parcelName.c_str(),
name.c_str(), EmitCppType().c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("const %s *%s = reinterpret_cast<const %s*>(%s.ReadUnpadBuffer(sizeof(%s)));\n",
EmitCppType().c_str(), name.c_str(), EmitCppType().c_str(), parcelName.c_str(), EmitCppType().c_str());
sb.Append(prefix).AppendFormat("if (%s == nullptr) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUnionTypeEmitter::EmitMemoryRecycle(
const std::string &name, bool ownership, StringBuilder &sb, const std::string &prefix) const
{
if (ownership) {
sb.Append(prefix).AppendFormat("if (%s != NULL) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("OsalMemFree(%s);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = NULL;\n", name.c_str());
sb.Append(prefix).Append("}\n");
}
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_UNIONTYPE_EMITTER_H
#define OHOS_IDL_HDI_UNIONTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiUnionTypeEmitter : public HdiTypeEmitter {
public:
inline void AddMember(std::string name, const AutoPtr<HdiTypeEmitter> &memberEmiter)
{
members_.push_back(std::make_tuple(name, memberEmiter));
}
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
std::string EmitCTypeDecl() const override;
std::string EmitCppTypeDecl() const override;
std::string EmitJavaTypeDecl() const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb,
const std::string &prefix) const override;
private:
std::vector<std::tuple<std::string, AutoPtr<HdiTypeEmitter>>> members_;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_UNIONTYPE_EMITTER_H

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hdi_ushort_type_emitter.h"
namespace OHOS {
namespace Idl {
TypeKind HdiUshortTypeEmitter::GetTypeKind()
{
return TypeKind::TYPE_USHORT;
}
std::string HdiUshortTypeEmitter::EmitCType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint16_t";
case TypeMode::PARAM_IN:
return "uint16_t";
case TypeMode::PARAM_OUT:
return "uint16_t*";
case TypeMode::LOCAL_VAR:
return "uint16_t";
default:
return "unknow type";
}
}
std::string HdiUshortTypeEmitter::EmitCppType(TypeMode mode) const
{
switch (mode) {
case TypeMode::NO_MODE:
return "uint16_t";
case TypeMode::PARAM_IN:
return "uint16_t";
case TypeMode::PARAM_OUT:
return "uint16_t&";
case TypeMode::LOCAL_VAR:
return "uint16_t";
default:
return "unknow type";
}
}
std::string HdiUshortTypeEmitter::EmitJavaType(TypeMode mode, bool isInnerType) const
{
// unsupported type
return "/";
}
void HdiUshortTypeEmitter::EmitCWriteVar(TypeMode mode, const std::string &name,
const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint16(%s, %s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint16(%s, %s)) {\n", replyParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint16(%s, %s)) {\n", dataParcelName_.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("%s = HDF_ERR_INVALID_PARAM;\n", errorCodeName_.c_str());
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCppWriteVar(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel) const
{
std::string parcelName = (mode == TypeMode::PARAM_IN) ? dataParcelName_ : replyParcelName_;
if ((mode == TypeMode::PARAM_IN) || (innerLevel > 0)) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return HDF_ERR_INVALID_PARAM;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufWriteUint16(data, %s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix, std::vector<std::string> &freeObjStatements) const
{
sb.Append(prefix).AppendFormat("if (!HdfSbufReadUint16(data, &%s)) {\n", name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
EmitFreeStatements(freeObjStatements, sb, prefix + TAB);
sb.Append(prefix + TAB).AppendFormat("goto %s;\n", gotoLabel.c_str());
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCppMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
sb.Append(prefix).AppendFormat("if (!%s.WriteUint16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: write %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
void HdiUshortTypeEmitter::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name,
StringBuilder &sb, const std::string &prefix, unsigned int innerLevel) const
{
if (innerLevel > 0) {
sb.Append(prefix).AppendFormat("%s %s = 0;\n", EmitCppType().c_str(), name.c_str());
}
sb.Append(prefix).AppendFormat("if (!%s.ReadUint16(%s)) {\n", parcelName.c_str(), name.c_str());
sb.Append(prefix + TAB).AppendFormat("HDF_LOGE(\"%%{public}s: read %s failed!\", __func__);\n", name.c_str());
sb.Append(prefix + TAB).Append("return false;\n");
sb.Append(prefix).Append("}\n");
}
} // namespace Idl
} // namespace OHOS

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IDL_HDI_USHORTTYPE_EMITTER_H
#define OHOS_IDL_HDI_USHORTTYPE_EMITTER_H
#include "codegen/HDI/hdi_type_emitter.h"
namespace OHOS {
namespace Idl {
class HdiUshortTypeEmitter : public HdiTypeEmitter {
public:
TypeKind GetTypeKind() override;
std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
StringBuilder &sb, const std::string &prefix) const override;
void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix) const override;
void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
TypeMode mode, unsigned int innerLevel = 0) const override;
void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
const std::string &prefix, unsigned int innerLevel = 0) const override;
};
} // namespace Idl
} // namespace OHOS
#endif // OHOS_IDL_HDI_USHORTTYPE_EMITTER_H