warning clear.

Signed-off-by: zhangjidong <873721519@qq.com>
This commit is contained in:
zhangjidong 2024-04-20 11:09:10 +08:00
parent 4d6d3198f8
commit e31cff0ef5
16 changed files with 316 additions and 203 deletions

View File

@ -62,7 +62,7 @@ String ASTMethod::Dump(const String& prefix)
sb.Append(prefix);
sb.Append(returnType_->ToString()).Append(' ');
sb.Append(name_).Append('(');
if (parameters_.size() != 0) {
if (parameters_.size() > 0) {
sb.Append('\n');
for (auto parameter : parameters_) {
String info = parameter->Dump(prefix + " ");

View File

@ -637,20 +637,8 @@ void CppCodeEmitter::EmitInterfaceStubMethodImpls(StringBuilder& sb, const Strin
sb.Append(prefix).Append("}\n");
}
void CppCodeEmitter::EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix)
void CppCodeEmitter::EmitInterfaceStubMethodImplReturn(MetaMethod* mm, StringBuilder& sb, const String& prefix, MetaType* returnType)
{
sb.Append(prefix).AppendFormat("case COMMAND_%s: {\n", ConstantName(mm->name_).string());
for (int i = 0; i < mm->parameterNumber_; i++) {
MetaParameter* mp = mm->parameters_[i];
if ((mp->attributes_ & ATTR_IN) != 0) {
MetaType* mt = metaComponent_->types_[mp->typeIndex_];
const std::string name = mp->name_;
EmitReadVariable("data.", name, mt, sb, prefix + TAB);
} else if ((mp->attributes_ & ATTR_OUT) != 0) {
EmitLocalVariable(mp, sb, prefix + TAB);
}
}
MetaType* returnType = metaComponent_->types_[mm->returnTypeIndex_];
if (returnType->kind_ != TypeKind::Void) {
if ((returnType->kind_ == TypeKind::Sequenceable) || (returnType->kind_ == TypeKind::Interface)) {
sb.Append(prefix + TAB).AppendFormat("%s result = nullptr;\n",
@ -686,6 +674,24 @@ void CppCodeEmitter::EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder&
}
sb.AppendFormat(");\n", mm->name_);
}
}
void CppCodeEmitter::EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix)
{
sb.Append(prefix).AppendFormat("case COMMAND_%s: {\n", ConstantName(mm->name_).string());
for (int i = 0; i < mm->parameterNumber_; i++) {
MetaParameter* mp = mm->parameters_[i];
if ((mp->attributes_ & ATTR_IN) != 0) {
MetaType* mt = metaComponent_->types_[mp->typeIndex_];
const std::string name = mp->name_;
EmitReadVariable("data.", name, mt, sb, prefix + TAB);
} else if ((mp->attributes_ & ATTR_OUT) != 0) {
EmitLocalVariable(mp, sb, prefix + TAB);
}
}
MetaType* returnType = metaComponent_->types_[mm->returnTypeIndex_];
EmitInterfaceStubMethodImplReturn(mm, sb, prefix, returnType);
sb.Append(prefix + TAB).Append("if (!reply.WriteInt32(errCode)) {\n");
if (logOn_) {
sb.Append(prefix + TAB).Append(TAB).Append("HiLog::Error(LABEL, \"Write Int32 failed!\");\n");
@ -800,6 +806,16 @@ void CppCodeEmitter::EmitWriteVariable(const String& parcelName, const std::stri
sb.Append(prefix).Append(TAB).Append("return ERR_INVALID_DATA;\n");
sb.Append(prefix).Append("}\n");
break;
default:
EmitWriteVariableFloat(parcelName, name, mt, sb, prefix);
break;
}
}
void CppCodeEmitter::EmitWriteVariableFloat(
const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb, const String& prefix)
{
switch (mt->kind_) {
case TypeKind::Float:
sb.Append(prefix).AppendFormat("if (!%sWriteFloat(%s)) {\n", parcelName.string(), name.c_str());
if (logOn_) {
@ -845,6 +861,9 @@ void CppCodeEmitter::EmitWriteVariableComplex(
sb.Append(prefix).AppendFormat("%sWriteInt32(%s.size());\n", parcelName.string(), name.c_str());
sb.Append(prefix).AppendFormat("for (auto it = %s.begin(); it != %s.end(); ++it) {\n",
name.c_str(), name.c_str());
if (mt == nullptr) {
break;
}
MetaType* innerType = metaComponent_->types_[mt->nestedTypeIndexes_[0]];
EmitWriteVariable(parcelName, "(*it)", innerType, sb, prefix + TAB);
sb.Append(prefix).Append("}\n");
@ -959,6 +978,16 @@ void CppCodeEmitter::EmitReadVariable(const String& parcelName, const std::strin
sb.Append(prefix).AppendFormat("%s = %sReadInt64();\n", name.c_str(), parcelName.string());
}
break;
default:
EmitReadVariableFloat(parcelName, name, mt, sb, prefix, emitType);
break;
}
}
void CppCodeEmitter::EmitReadVariableFloat(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& sb, const String& prefix, bool emitType)
{
switch (mt->kind_) {
case TypeKind::Float:
if (emitType) {
sb.Append(prefix).AppendFormat("%s %s = %sReadFloat();\n",
@ -979,6 +1008,7 @@ void CppCodeEmitter::EmitReadVariable(const String& parcelName, const std::strin
EmitReadVariableComplex(parcelName, name, mt, sb, prefix, emitType);
break;
}
}
void CppCodeEmitter::EmitReadVariableComplex(const String& parcelName, const std::string& name, MetaType* mt,
@ -1182,6 +1212,15 @@ String CppCodeEmitter::EmitType(MetaType* mt, unsigned int attributes, bool isIn
} else {
return "long&";
}
default:
return EmitFloatType(mt, attributes, isInnerType);
}
}
String CppCodeEmitter::EmitFloatType(MetaType* mt, unsigned int attributes, bool isInnerType)
{
switch (mt->kind_) {
case TypeKind::Float:
enteredVector_ = false;
if (attributes & ATTR_IN) {
@ -1202,6 +1241,7 @@ String CppCodeEmitter::EmitType(MetaType* mt, unsigned int attributes, bool isIn
default:
return EmitComplexType(mt, attributes, isInnerType);
}
}
String CppCodeEmitter::EmitComplexType(MetaType* mt, unsigned int attributes, bool isInnerType)

View File

@ -103,6 +103,9 @@ private:
void EmitInterfaceStubMethodImpls(StringBuilder& sb, const String& prefix);
void EmitInterfaceStubMethodImplReturn(MetaMethod* mm, StringBuilder& sb, const String& prefix,
MetaType* returnType);
void EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix);
void EmitInterfaceMethodCommands(StringBuilder& sb, const String& prefix);
@ -119,6 +122,9 @@ private:
void EmitWriteVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix);
void EmitWriteVariableFloat(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix);
void EmitWriteVariableComplex(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix);
@ -129,6 +135,9 @@ private:
void EmitReadVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix, bool emitType = true);
void EmitReadVariableFloat(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix, bool emitType = true);
void EmitReadVariableComplex(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
const String& prefix, bool emitType = true);
@ -144,6 +153,8 @@ private:
String EmitType(MetaType* mt, unsigned int attributes, bool isInnerType);
String EmitFloatType(MetaType* mt, unsigned int attributes, bool isInnerType);
String EmitComplexType(MetaType* mt, unsigned int attributes, bool isInnerType);
String EmitListType(MetaType* mt, unsigned int attributes, bool isInnerType);

View File

@ -190,7 +190,7 @@ bool RustCodeEmitter::AppendRealPath(StringBuilder& sb, const String& fpnpp)
bool RustCodeEmitter::EmitCustomHeaders(StringBuilder& sb)
{
bool custom = false;
uint32_t custom = false;
for (int i = 0; i < metaComponent_->sequenceableNumber_; i++) {
MetaSequenceable* ms = metaComponent_->sequenceables_[i];
custom |= AppendRealPath(sb, String(ms->namespace_) + String(ms->name_));
@ -202,7 +202,7 @@ bool RustCodeEmitter::EmitCustomHeaders(StringBuilder& sb)
custom |= AppendRealPath(sb, String(mi->namespace_) + String(mi->name_));
}
}
return custom;
return static_cast<bool>(custom);
}
void RustCodeEmitter::EmitCommands(StringBuilder& sb)

View File

@ -151,6 +151,15 @@ void TsCodeEmitter::EmitInterfaceMethod(MetaMethod* metaMethod, StringBuilder& s
method.parameters_.emplace_back(para);
}
EmitInterfaceMethodLastPara(metaMethod, prefix, method, methodStr, haveOutPara);
methodStr.Append("): void;\n");
stringBuilder.Append(methodStr.ToString());
methods_.emplace_back(method);
}
void TsCodeEmitter::EmitInterfaceMethodLastPara(MetaMethod* metaMethod, const String& prefix, Method& method,
StringBuilder& methodStr, bool haveOutPara)
{
bool isLastParaTypeIn = false;
for (size_t index = 0; index < method.parameters_.size(); index++) {
if ((method.parameters_[index].attr_ & ATTR_IN) != 0) {
@ -192,9 +201,6 @@ void TsCodeEmitter::EmitInterfaceMethod(MetaMethod* metaMethod, StringBuilder& s
}
}
}
methodStr.Append("): void;\n");
stringBuilder.Append(methodStr.ToString());
methods_.emplace_back(method);
}
void TsCodeEmitter::EmitInterfaceMethodParameter(MetaParameter* mp, StringBuilder& stringBuilder, const String& prefix)
@ -522,13 +528,14 @@ void TsCodeEmitter::EmitInterfaceStubMethodImpls(StringBuilder& stringBuilder, c
stringBuilder.Append(prefix).AppendFormat("%s(", MethodName(metaMethod->name_).string());
for (int index = 0; index < metaMethod->parameterNumber_; index++) {
MetaParameter* mp = metaMethod->parameters_[index];
if ((mp->attributes_ & ATTR_IN) != 0) {
EmitInterfaceMethodParameter(mp, stringBuilder, prefix + TAB);
if (index != metaMethod->parameterNumber_ - 1) {
stringBuilder.Append(", ");
} else {
isLastParaTypeIn = true;
}
if ((mp->attributes_ & ATTR_IN) == 0) {
continue;
}
EmitInterfaceMethodParameter(mp, stringBuilder, prefix + TAB);
if (index != metaMethod->parameterNumber_ - 1) {
stringBuilder.Append(", ");
} else {
isLastParaTypeIn = true;
}
}
if (!isLastParaTypeIn) {
@ -647,8 +654,7 @@ void TsCodeEmitter::EmitLicense(StringBuilder& stringBuilder)
}
void TsCodeEmitter::EmitWriteVariable(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& stringBuilder,
const String& prefix)
StringBuilder& stringBuilder, const String& prefix)
{
switch (mt->kind_) {
case TypeKind::Boolean:
@ -669,6 +675,16 @@ void TsCodeEmitter::EmitWriteVariable(const String& parcelName, const std::strin
case TypeKind::Double:
stringBuilder.Append(prefix).AppendFormat("%s.writeDouble(%s);\n", parcelName.string(), name.c_str());
break;
default:
EmitWriteVariableObject(parcelName, name, mt, stringBuilder, prefix);
break;
}
}
void TsCodeEmitter::EmitWriteVariableObject(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& stringBuilder, const String& prefix)
{
switch (mt->kind_) {
case TypeKind::String:
stringBuilder.Append(prefix).AppendFormat("%s.writeString(%s);\n", parcelName.string(), name.c_str());
break;
@ -792,6 +808,16 @@ void TsCodeEmitter::EmitReadVariable(const String& parcelName, const std::string
case TypeKind::Double:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readDouble();\n", name.c_str(), parcelName.string());
break;
default:
EmitReadVariableObject(parcelName, name, mt, attributes, stringBuilder, prefix);
break;
}
}
void TsCodeEmitter::EmitReadVariableObject(const String& parcelName, const std::string& name, MetaType* mt,
unsigned int attributes, StringBuilder& stringBuilder, const String& prefix)
{
switch (mt->kind_) {
case TypeKind::String:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readString();\n", name.c_str(), parcelName.string());
break;
@ -844,40 +870,16 @@ void TsCodeEmitter::EmitReadArrayVariable(const String& parcelName, const std::s
MetaType* innerType = metaComponent_->types_[mt->nestedTypeIndexes_[0]];
switch (innerType->kind_) {
case TypeKind::Boolean:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readBooleanArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Char:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readCharArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Byte:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readByteArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Short:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readShortArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Integer:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readIntArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Long:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readLongArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Float:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readFloatArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Double:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readDoubleArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::String:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readStringArray();\n", name.c_str(),
parcelName.string());
stringBuilder.Append(prefix).AppendFormat("let %s = %s.%s();\n", name.c_str(),
parcelName.string(), typekind_read_array_[innerType->kind_].c_str());
break;
case TypeKind::Sequenceable: {
String typeName = EmitType(mt).EndsWith("]") ?
@ -908,40 +910,16 @@ void TsCodeEmitter::EmitReadOutArrayVariable(const String& parcelName, const std
MetaType* innerType = metaComponent_->types_[mt->nestedTypeIndexes_[0]];
switch (innerType->kind_) {
case TypeKind::Boolean:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readBooleanArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Char:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readCharArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Byte:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readByteArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Short:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readShortArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Integer:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readIntArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Long:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readLongArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Float:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readFloatArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::Double:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readDoubleArray();\n", name.c_str(),
parcelName.string());
break;
case TypeKind::String:
stringBuilder.Append(prefix).AppendFormat("let %s = %s.readStringArray();\n", name.c_str(),
parcelName.string());
stringBuilder.Append(prefix).AppendFormat("let %s = %s.%s();\n", name.c_str(),
parcelName.string(), typekind_read_array_[innerType->kind_].c_str());
break;
case TypeKind::Sequenceable: {
stringBuilder.Append(prefix).AppendFormat("let %sSize = %s.readInt();\n", name.c_str(),

View File

@ -18,6 +18,7 @@
#include <string>
#include <vector>
#include <unordered_map>
#include "codegen/code_emitter.h"
#include "util/string_builder.h"
@ -72,6 +73,9 @@ private:
void EmitInterfaceMethod(MetaMethod* metaMethod, StringBuilder& stringBuilder, const String& prefix);
void EmitInterfaceMethodLastPara(MetaMethod* metaMethod, const String& prefix, Method& method,
StringBuilder& methodStr, bool haveOutPara);
void EmitInterfaceMethodCallback(
MetaMethod* metaMethod, int methodIndex, StringBuilder& stringBuilder, const String& prefix, bool haveOutPara);
@ -120,12 +124,16 @@ private:
void EmitLicense(StringBuilder& stringBuilder);
void EmitWriteVariable(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& stringBuilder,
const String& prefix);
StringBuilder& stringBuilder, const String& prefix);
void EmitWriteVariableObject(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& stringBuilder, const String& prefix);
void EmitReadVariable(const String& parcelName, const std::string& name, MetaType* mt, unsigned int attributes,
StringBuilder& stringBuilder,
const String& prefix);
StringBuilder& stringBuilder, const String& prefix);
void EmitReadVariableObject(const String& parcelName, const std::string& name, MetaType* mt, unsigned int attributes,
StringBuilder& stringBuilder, const String& prefix);
void EmitReadOutVariable(const String& parcelName, const std::string& name, MetaType* mt,
StringBuilder& stringBuilder,
@ -161,6 +169,18 @@ private:
const std::string SuffixAdded(const String& name);
std::vector<Method> methods_;
std::unordered_map<TypeKind, std::string> typekind_read_array_ = {
{TypeKind::Boolean, "readBooleanArray"},
{TypeKind::Char, "readCharArray"},
{TypeKind::Byte, "readByteArray"},
{TypeKind::Short, "readShortArray"},
{TypeKind::Integer, "readIntArray"},
{TypeKind::Long, "readLongArray"},
{TypeKind::Float, "readFloatArray"},
{TypeKind::Double, "readDoubleArray"},
{TypeKind::String, "readStringArray"},
};
};
} // namespace Idl
} // namespace OHOS

View File

@ -39,7 +39,7 @@ struct MetaComponent {
MetaSequenceable** sequenceables_;
MetaInterface** interfaces_;
MetaType** types_;
int stringPoolSize_;
size_t stringPoolSize_;
char* stringPool_;
};

View File

@ -60,10 +60,10 @@ size_t MetadataBuilder::CalculateMetadataSize()
void MetadataBuilder::CalculateMetaComponent(ASTModule* module)
{
int namespaceNumber = module->GetNamespaceNumber();
int sequenceableNumber = module->GetSequenceableNumber();
int interfaceNumber = module->GetInterfaceNumber();
int typeNumber = module->GetTypeNumber();
size_t namespaceNumber = module->GetNamespaceNumber();
size_t sequenceableNumber = module->GetSequenceableNumber();
size_t interfaceNumber = module->GetInterfaceNumber();
size_t typeNumber = module->GetTypeNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -79,15 +79,15 @@ void MetadataBuilder::CalculateMetaComponent(ASTModule* module)
// stringPool_'s address
baseAddr_ = baseAddr_ + sizeof(MetaType*) * typeNumber;
for (int i = 0; i < namespaceNumber; i++) {
for (size_t i = 0; i < namespaceNumber; i++) {
CalculateMetaNamespace(module->GetNamespace(i));
}
for (int i = 0; i < sequenceableNumber; i++) {
for (size_t i = 0; i < sequenceableNumber; i++) {
CalculateMetaSequenceable(module->GetSequenceable(i));
}
for (int i = 0; i < interfaceNumber; i++) {
for (size_t i = 0; i < interfaceNumber; i++) {
CalculateMetaInterface(module->GetInterface(i));
}
@ -102,9 +102,9 @@ void MetadataBuilder::CalculateMetaComponent(ASTModule* module)
void MetadataBuilder::CalculateMetaNamespace(ASTNamespace* nspace)
{
int sequenceableNumber = nspace->GetSequenceableNumber();
int interfaceNumber = nspace->GetInterfaceNumber();
int namespaceNumber = nspace->GetNamespaceNumber();
size_t sequenceableNumber = nspace->GetSequenceableNumber();
size_t interfaceNumber = nspace->GetInterfaceNumber();
size_t namespaceNumber = nspace->GetNamespaceNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -118,7 +118,7 @@ void MetadataBuilder::CalculateMetaNamespace(ASTNamespace* nspace)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaNamespace*) * namespaceNumber;
for (int i = 0; i < namespaceNumber; i++) {
for (size_t i = 0; i < namespaceNumber; i++) {
CalculateMetaNamespace(nspace->GetNamespace(i));
}
}
@ -135,7 +135,7 @@ void MetadataBuilder::CalculateMetaSequenceable(ASTSequenceableType* sequenceabl
void MetadataBuilder::CalculateMetaInterface(ASTInterfaceType* interface)
{
int methodNumber = interface->GetMethodNumber();
size_t methodNumber = interface->GetMethodNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -147,14 +147,14 @@ void MetadataBuilder::CalculateMetaInterface(ASTInterfaceType* interface)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaMethod*) * methodNumber;
for (int i = 0; i < methodNumber; i++) {
for (size_t i = 0; i < methodNumber; i++) {
CalculateMetaMethod(interface->GetMethod(i));
}
}
void MetadataBuilder::CalculateMetaMethod(ASTMethod* method)
{
int parameterNumber = method->GetParameterNumber();
size_t parameterNumber = method->GetParameterNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -165,7 +165,7 @@ void MetadataBuilder::CalculateMetaMethod(ASTMethod* method)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaParameter*) * parameterNumber;
for (int i = 0; i < parameterNumber; i++) {
for (size_t i = 0; i < parameterNumber; i++) {
CalculateMetaParameter(method->GetParameter(i));
}
}
@ -191,7 +191,7 @@ void MetadataBuilder::CalculateMetaType(ASTType* type)
baseAddr_ = baseAddr_ + sizeof(int*);
} else if (type->IsMapType()) {
// end address
int typeNumber = 2;
size_t typeNumber = 2;
baseAddr_ = baseAddr_ + sizeof(int*) * typeNumber;
} else if (type->IsArrayType()) {
baseAddr_ = baseAddr_ + sizeof(int*);
@ -214,16 +214,16 @@ void MetadataBuilder::WriteMetadata(uintptr_t base)
void MetadataBuilder::WriteMetaComponent(ASTModule* module)
{
int namespaceNumber = module->GetNamespaceNumber();
int sequenceableNumber = module->GetSequenceableNumber();
int interfaceNumber = module->GetInterfaceNumber();
int typeNumber = module->GetTypeNumber();
size_t namespaceNumber = module->GetNamespaceNumber();
size_t sequenceableNumber = module->GetSequenceableNumber();
size_t interfaceNumber = module->GetInterfaceNumber();
size_t typeNumber = module->GetTypeNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
MetaComponent* mc = reinterpret_cast<MetaComponent*>(baseAddr_);
mc->magic_ = METADATA_MAGIC_NUMBER;
mc->size_ = size_;
mc->size_ = static_cast<int>(size_);
mc->namespaceNumber_ = namespaceNumber;
mc->sequenceableNumber_ = sequenceableNumber;
mc->interfaceNumber_ = interfaceNumber;
@ -250,15 +250,15 @@ void MetadataBuilder::WriteMetaComponent(ASTModule* module)
mc->name_ = WriteString(module->GetName());
for (int i = 0; i < namespaceNumber; i++) {
for (size_t i = 0; i < namespaceNumber; i++) {
mc->namespaces_[i] = WriteMetaNamespace(module->GetNamespace(i));
}
for (int i = 0; i < sequenceableNumber; i++) {
for (size_t i = 0; i < sequenceableNumber; i++) {
mc->sequenceables_[i] = WriteMetaSequenceable(module->GetSequenceable(i));
}
for (int i = 0; i < interfaceNumber; i++) {
for (size_t i = 0; i < interfaceNumber; i++) {
mc->interfaces_[i] = WriteMetaInterface(module->GetInterface(i));
}
@ -271,9 +271,9 @@ void MetadataBuilder::WriteMetaComponent(ASTModule* module)
MetaNamespace* MetadataBuilder::WriteMetaNamespace(ASTNamespace* nspace)
{
int sequenceableNumber = nspace->GetSequenceableNumber();
int interfaceNumber = nspace->GetInterfaceNumber();
int namespaceNumber = nspace->GetNamespaceNumber();
size_t sequenceableNumber = nspace->GetSequenceableNumber();
size_t interfaceNumber = nspace->GetInterfaceNumber();
size_t namespaceNumber = nspace->GetNamespaceNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -294,17 +294,17 @@ MetaNamespace* MetadataBuilder::WriteMetaNamespace(ASTNamespace* nspace)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaNamespace*) * namespaceNumber;
for (int i = 0; i < sequenceableNumber; i++) {
for (size_t i = 0; i < sequenceableNumber; i++) {
AutoPtr<ASTSequenceableType> sequenceable = nspace->GetSequenceable(i);
mn->sequenceableIndexes_[i] = module_->IndexOf(sequenceable);
}
for (int i = 0; i < interfaceNumber; i++) {
for (size_t i = 0; i < interfaceNumber; i++) {
AutoPtr<ASTInterfaceType> interface = nspace->GetInterface(i);
mn->interfaceIndexes_[i] = module_->IndexOf(interface);
}
for (int i = 0; i < namespaceNumber; i++) {
for (size_t i = 0; i < namespaceNumber; i++) {
AutoPtr<ASTNamespace> inner = nspace->GetNamespace(i);
mn->namespaces_[i] = WriteMetaNamespace(inner);
}
@ -327,7 +327,7 @@ MetaSequenceable* MetadataBuilder::WriteMetaSequenceable(ASTSequenceableType* pa
MetaInterface* MetadataBuilder::WriteMetaInterface(ASTInterfaceType* interface)
{
int methodNumber = interface->GetMethodNumber();
size_t methodNumber = interface->GetMethodNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -344,7 +344,7 @@ MetaInterface* MetadataBuilder::WriteMetaInterface(ASTInterfaceType* interface)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaMethod*) * methodNumber;
for (int i = 0; i < methodNumber; i++) {
for (size_t i = 0; i < methodNumber; i++) {
mi->methods_[i] = WriteMetaMethod(interface->GetMethod(i));
}
@ -353,7 +353,7 @@ MetaInterface* MetadataBuilder::WriteMetaInterface(ASTInterfaceType* interface)
MetaMethod* MetadataBuilder::WriteMetaMethod(ASTMethod* method)
{
int parameterNumber = method->GetParameterNumber();
size_t parameterNumber = method->GetParameterNumber();
// begin address
baseAddr_ = ALIGN8(baseAddr_);
@ -369,7 +369,7 @@ MetaMethod* MetadataBuilder::WriteMetaMethod(ASTMethod* method)
// end address
baseAddr_ = baseAddr_ + sizeof(MetaParameter*) * parameterNumber;
for (int i = 0; i < parameterNumber; i++) {
for (size_t i = 0; i < parameterNumber; i++) {
mm->parameters_[i] = WriteMetaParameter(method->GetParameter(i));
}
@ -419,7 +419,7 @@ MetaType* MetadataBuilder::WriteMetaType(ASTType* type)
// end address
baseAddr_ = baseAddr_ + sizeof(int*);
} else if (type->IsMapType()) {
int typeNumber = 2;
size_t typeNumber = 2;
mt->nestedTypeNumber_ = typeNumber;
// nestedTypeIndexes_'s address
baseAddr_ = ALIGN8(baseAddr_);

View File

@ -19,7 +19,7 @@
#include <memory>
#include "metadata/meta_component.h"
#include "util/string.h"
#include <stdlib.h>
#include <cstdlib>
namespace OHOS {
namespace Idl {

View File

@ -94,33 +94,21 @@ Token Lexer::ReadToken(bool skipComment)
}
switch (c) {
case '<':
currentToken_ = Token::ANGLE_BRACKETS_LEFT;
return currentToken_;
case '>':
currentToken_ = Token::ANGLE_BRACKETS_RIGHT;
return currentToken_;
case '{':
currentToken_ = Token::BRACES_LEFT;
return currentToken_;
case '}':
currentToken_ = Token::BRACES_RIGHT;
return currentToken_;
case '[':
currentToken_ = Token::BRACKETS_LEFT;
return currentToken_;
case ']':
currentToken_ = Token::BRACKETS_RIGHT;
return currentToken_;
case ',':
currentToken_ = Token::COMMA;
case '(':
case ')':
case '.':
case ';':
currentToken_ = token_map_[c];
return currentToken_;
case '/':
if (currentFile_->PeekChar() == '/') {
ReadLineComment(c);
if (!skipComment) {
return currentToken_;
}
continue;
} else if (currentFile_->PeekChar() == '*') {
ReadBlockComment(c);
if (!skipComment) {
@ -128,20 +116,20 @@ Token Lexer::ReadToken(bool skipComment)
}
continue;
}
if (currentFile_->PeekChar() == '/' && !skipComment) {
return currentToken_;
}
if (currentFile_->PeekChar() == '/' && skipComment) {
continue;
}
if (currentFile_->PeekChar() == '*' && !skipComment) {
return currentToken_;
}
if (currentFile_->PeekChar() == '*' && skipComment) {
continue;
}
currentToken_ = Token::UNKNOWN;
return currentToken_;
case '(':
currentToken_ = Token::PARENTHESES_LEFT;
return currentToken_;
case ')':
currentToken_ = Token::PARENTHESES_RIGHT;
return currentToken_;
case '.':
currentToken_ = Token::DOT;
return currentToken_;
case ';':
currentToken_ = Token::SEMICOLON;
return currentToken_;
default:
currentToken_ = Token::UNKNOWN;
return currentToken_;

View File

@ -106,6 +106,19 @@ private:
String comment_;
bool havePeek_ = false;
std::shared_ptr<File> currentFile_;
std::unordered_map<char, Token> token_map_ = {
{'<', Token::ANGLE_BRACKETS_LEFT},
{'>', Token::ANGLE_BRACKETS_RIGHT},
{'{', Token::BRACES_LEFT},
{'}', Token::BRACES_RIGHT},
{'[', Token::BRACKETS_LEFT},
{']', Token::BRACKETS_RIGHT},
{',', Token::COMMA},
{'(', Token::PARENTHESES_LEFT},
{')', Token::PARENTHESES_RIGHT},
{'.', Token::DOT},
{';', Token::SEMICOLON},
};
};
} // namespace Idl
} // namespace OHOS

View File

@ -216,12 +216,8 @@ bool Parser::ParseInterface()
}
}
bool Parser::ParseMethod(ASTInterfaceType* interface)
void Parser::ParseMethodPeek(Token& token, bool& oneway, bool& ret)
{
bool ret = true;
bool oneway = false;
Token token;
token = lexer_.PeekToken();
if (token == Token::BRACKETS_LEFT) {
lexer_.GetToken();
@ -247,7 +243,10 @@ bool Parser::ParseMethod(ASTInterfaceType* interface)
lexer_.GetToken();
}
}
AutoPtr<ASTType> type = ParseType();
}
bool Parser::ParseMethodName(Token& token, ASTType* type, ASTInterfaceType* interface)
{
if (type == nullptr) {
token = lexer_.PeekToken();
if (token != Token::BRACES_RIGHT) {
@ -285,12 +284,11 @@ bool Parser::ParseMethod(ASTInterfaceType* interface)
}
return false;
}
token = lexer_.GetToken();
return true;
}
AutoPtr<ASTMethod> method = new ASTMethod();
method->SetName(lexer_.GetIdentifier());
method->SetOneway(oneway);
method->SetReturnType(type);
bool Parser::ParseMethodBrackets(Token& token, ASTMethod* method, bool& ret)
{
if (method->IsOneway()) {
if (!method->GetReturnType()->IsVoidType()) {
LogError(token, String("void return type expected in oneway method."));
@ -300,16 +298,17 @@ bool Parser::ParseMethod(ASTInterfaceType* interface)
token = lexer_.PeekToken();
if (token != Token::PARENTHESES_LEFT) {
LogError(token, String("\"(\" is expected."));
if (token != Token::BRACES_RIGHT) {
// jump over colon
lexer_.GetToken();
while (token != Token::SEMICOLON && token != Token::END_OF_FILE) {
token = lexer_.PeekToken();
if (token == Token::BRACES_RIGHT) {
break;
}
lexer_.GetToken();
if (token == Token::BRACES_RIGHT) {
return false;
}
// jump over colon
lexer_.GetToken();
while (token != Token::SEMICOLON && token != Token::END_OF_FILE) {
token = lexer_.PeekToken();
if (token == Token::BRACES_RIGHT) {
break;
}
lexer_.GetToken();
}
return false;
}
@ -324,6 +323,29 @@ bool Parser::ParseMethod(ASTInterfaceType* interface)
token = lexer_.PeekToken();
}
}
return true;
}
bool Parser::ParseMethod(ASTInterfaceType* interface)
{
bool ret = true;
bool oneway = false;
Token token;
ParseMethodPeek(token, oneway, ret);
AutoPtr<ASTType> type = ParseType();
if (!ParseMethodName(token, type, interface)) {
return false;
}
token = lexer_.GetToken();
AutoPtr<ASTMethod> method = new ASTMethod();
method->SetName(lexer_.GetIdentifier());
method->SetOneway(oneway);
method->SetReturnType(type);
if (!ParseMethodBrackets(token, method, ret)) {
return false;
}
if (interface->IsOneway() || method->IsOneway()) {
for (size_t i = 0; i< method->GetParameterNumber(); i++) {
@ -355,9 +377,8 @@ bool Parser::ParseMethod(ASTInterfaceType* interface)
return ret;
}
bool Parser::ParseParameter(ASTMethod* method)
bool Parser::ParseParameterPeek(Token& token)
{
Token token = lexer_.PeekToken();
if (token != Token::BRACKETS_LEFT) {
LogError(token, String("\"[\" is expected."));
// jump to ',' or ')'
@ -367,10 +388,11 @@ bool Parser::ParseParameter(ASTMethod* method)
}
return false;
}
lexer_.GetToken();
AutoPtr<ASTParameter> parameter = new ASTParameter();
return true;
}
bool Parser::ParseParameterInOut(Token& token, ASTParameter* parameter)
{
token = lexer_.PeekToken();
while (token != Token::BRACKETS_RIGHT && token != Token::END_OF_FILE) {
switch (token) {
@ -407,6 +429,23 @@ bool Parser::ParseParameter(ASTMethod* method)
return false;
}
}
return true;
}
bool Parser::ParseParameter(ASTMethod* method)
{
Token token = lexer_.PeekToken();
if (!ParseParameterPeek(token)) {
return false;
}
lexer_.GetToken();
AutoPtr<ASTParameter> parameter = new ASTParameter();
if (!ParseParameterInOut(token, parameter)) {
return false;
}
// read ']'
lexer_.GetToken();
@ -629,8 +668,8 @@ bool Parser::ParseSequenceable()
bool Parser::CheckIntegrity()
{
bool definedInterface = false;
int interfaceNumber = module_->GetInterfaceNumber();
for (int i = 0; i < interfaceNumber; i++) {
size_t interfaceNumber = module_->GetInterfaceNumber();
for (size_t i = 0; i < interfaceNumber; i++) {
if (!module_->GetInterface(i)->IsExternal()) {
definedInterface = true;
break;

View File

@ -61,8 +61,18 @@ private:
bool ParseMethod(ASTInterfaceType* interface);
void ParseMethodPeek(Token& token, bool& oneway, bool& ret);
bool ParseMethodName(Token& token, ASTType* type, ASTInterfaceType* interface);
bool ParseMethodBrackets(Token& token, ASTMethod* method, bool& ret);
bool ParseParameter(ASTMethod* method);
bool ParseParameterPeek(Token& token);
bool ParseParameterInOut(Token& token, ASTParameter* parameter);
AutoPtr<ASTNamespace> NameSpaceEmpty();
AutoPtr<ASTType> ParseType();

View File

@ -19,7 +19,7 @@ import subprocess
import argparse
def parse_args():
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--src-idl', help='idl source file')
parser.add_argument('--dst-path', help='the converted target path')
@ -33,13 +33,13 @@ def parse_args():
return arguments
def run_command(cmd, execution_path, input_arguments):
def run_command(cmd, execution_path, input_arguments):
print(" ".join(cmd) + " | execution_path: " + execution_path)
proc = subprocess.Popen(cmd, cwd=execution_path, stdout=subprocess.PIPE)
proc.wait()
def idl_gen_interface(input_arguments):
def idl_gen_interface(input_arguments):
(path, name) = os.path.split(input_arguments.idl_tool_path)
is_exists = os.path.exists(input_arguments.dst_path)
if not is_exists:

View File

@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>
#include <climits>
#include "securec.h"
namespace OHOS {
@ -110,6 +111,9 @@ int File::Read()
isError_ = ferror(fd_) != 0;
buffer_[count] = -1;
}
if (count > INT_MAX) {
count = INT_MAX - 1;
}
size_ = count;
position_ = 0;
return count != 0 ? count : -1;

View File

@ -113,7 +113,10 @@ String::String(const char* string)
if (string != nullptr) {
string_ = SharedData::ToString(SharedData::Allocate(strlen(string)));
if (string_ != nullptr) {
(void)strcpy_s(string_, strlen(string) + 1, string);
errno_t ret = strcpy_s(string_, strlen(string) + 1, string);
if (ret != EOK) {
Logger::E(String::TAG, "The String::String is error in strcpy_s.");
}
}
}
}
@ -392,8 +395,8 @@ bool String::StartsWith(const String& other) const
return true;
}
size_t count = other.GetLength();
if (count > (size_t)GetLength()) {
int count = other.GetLength();
if (count > GetLength()) {
return false;
}
@ -429,8 +432,8 @@ bool String::EndsWith(const String& other) const
return true;
}
size_t count = other.GetLength();
size_t len = GetLength();
int count = other.GetLength();
int len = GetLength();
if (count > len) {
return false;
}
@ -444,11 +447,11 @@ String String::ToLowerCase() const
return *this;
}
size_t size = GetLength();
for (size_t i = 0; i < size; i++) {
int size = GetLength();
for (int i = 0; i < size; i++) {
if (isupper(string_[i])) {
String newStr(string_);
for (size_t j = i; j < size; j++) {
for (int j = i; j < size; j++) {
newStr.string_[j] = tolower(newStr.string_[j]);
}
return newStr;
@ -463,11 +466,11 @@ String String::ToUpperCase() const
return *this;
}
size_t size = GetLength();
for (size_t i = 0; i < size; i++) {
int size = GetLength();
for (int i = 0; i < size; i++) {
if (islower(string_[i])) {
String newStr(string_);
for (size_t j = i; j < size; j++) {
for (int j = i; j < size; j++) {
newStr.string_[j] = toupper(newStr.string_[j]);
}
return newStr;
@ -500,17 +503,18 @@ String String::Replace(char oldChar, char newChar) const
return *this;
}
size_t size = GetLength();
for (size_t i = 0; i < size; i++) {
if (string_[i] == oldChar) {
String newStr(string_);
for (size_t j = i; j < size; j++) {
if (newStr.string_[j] == oldChar) {
newStr.string_[j] = newChar;
}
}
return newStr;
int size = GetLength();
for (int i = 0; i < size; i++) {
if (string_[i] != oldChar) {
continue;
}
String newStr(string_);
for (int j = i; j < size; j++) {
if (newStr.string_[j] == oldChar) {
newStr.string_[j] = newChar;
}
}
return newStr;
}
return *this;
}
@ -574,7 +578,10 @@ String& String::operator=(const char* string)
string_ = SharedData::ToString(SharedData::Allocate(strlen(string)));
if (string_ != nullptr) {
(void)strcpy_s(string_, strlen(string) + 1, string);
errno_t ret = strcpy_s(string_, strlen(string) + 1, string);
if (ret != EOK) {
Logger::E(String::TAG, "The operator= is error in strcpy_s.");
}
}
return *this;
}
@ -626,7 +633,10 @@ String String::operator+=(const String& other) const
String newString(newSize);
if (newString.string_ != nullptr) {
(void)memcpy_s(newString.string_, newSize + 1, string_, thisSize);
(void)strcpy_s(newString.string_ + thisSize, newSize + 1 - thisSize, other.string_);
errno_t ret = strcpy_s(newString.string_ + thisSize, newSize + 1 - thisSize, other.string_);
if (ret != EOK) {
Logger::E(String::TAG, "The operator+= is error in strcpy_s.");
}
}
return newString;
}