Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
Signed-off-by: Cervenka Dusan <[email protected]>
  • Loading branch information
Hadatko committed Jun 2, 2023
1 parent c2e072a commit 40ae66b
Show file tree
Hide file tree
Showing 50 changed files with 830 additions and 1,000 deletions.
85 changes: 23 additions & 62 deletions erpcgen/src/CGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ void CGenerator::generate()
m_templateData["structs"] = empty;
m_templateData["unions"] = empty;
m_templateData["consts"] = empty;
m_templateData["functions"] = empty;

m_templateData["nonExternalStructUnion"] = false;

Expand Down Expand Up @@ -573,7 +572,6 @@ void CGenerator::generate()
groupTemplate["includes"] = makeGroupIncludesTemplateData(group);
groupTemplate["symbolsMap"] = makeGroupSymbolsTemplateData(group);
groupTemplate["interfaces"] = makeGroupInterfacesTemplateData(group);
groupTemplate["callbacks"] = makeGroupCallbacksTemplateData(group);
group->setTemplate(groupTemplate);

generateGroupOutputFiles(group);
Expand Down Expand Up @@ -1002,43 +1000,6 @@ data_map CGenerator::makeGroupSymbolsTemplateData(Group *group)
return symbolsTemplate;
}

data_list CGenerator::makeGroupCallbacksTemplateData(Group *group)
{
data_list functionTypes;
std::map<FunctionType *, int> functionTypeMap;

// need go trough functions instead of group symbols.
for (Interface *interface : group->getInterfaces())
{
for (Function *function : interface->getFunctions())
{
FunctionType *functionType = function->getFunctionType();
if (functionType)
{
bool isPresent = (functionTypeMap.find(functionType) != functionTypeMap.end());
if (isPresent)
{
++functionTypeMap[functionType];
}
else
{
functionTypeMap[functionType] = 1;
}
}
}
}

for (std::map<FunctionType *, int>::iterator it = functionTypeMap.begin(); it != functionTypeMap.end(); ++it)
{
if (it->second > 1)
{
functionTypes.push_back(getFunctionTypeTemplateData(group, it->first));
}
}

return functionTypes;
}

data_map CGenerator::getStructDeclarationTemplateData(StructType *structType)
{
data_map info;
Expand Down Expand Up @@ -1681,7 +1642,7 @@ data_map CGenerator::getFunctionBaseTemplateData(Group *group, FunctionBase *fn)
return info;
}

data_map CGenerator::getFunctionTemplateData(Group *group, Function *fn, Interface *interface)
data_map CGenerator::getFunctionTemplateData(Group *group, Function *fn)
{
data_map info;

Expand Down Expand Up @@ -1724,22 +1685,18 @@ data_map CGenerator::getFunctionTemplateData(Group *group, Function *fn, Interfa
else
{
info["isCallback"] = false;
string serverProto = getFunctionServerCall(fn, nullptr, "m_handler->");
info["serverPrototype"] = serverProto;
info["serverPrototype"] = getFunctionServerCall(fn);
info["serviceId"] = "";
}
string serverProtoC = getFunctionServerCall(fn);
string serverProtoC = getFunctionServerCall(fn, true);
info["serverPrototypeC"] = serverProtoC;

string proto = getFunctionPrototype(group, fn);
info["prototype"] = proto;
if (interface != nullptr)
{
string protoCpp = getFunctionPrototype(group, fn, interface->getName() + "_client", "", true);
info["prototypeCpp"] = protoCpp;
string protoInterface = getFunctionPrototype(group, fn, "", "", true);
info["prototypeInterface"] = protoInterface;
}
string protoCpp = getFunctionPrototype(group, fn, fn->getInterface()->getName() + "_client", "", true);
info["prototypeCpp"] = protoCpp;
string protoInterface = getFunctionPrototype(group, fn, "", "", true);
info["prototypeInterface"] = protoInterface;

data_list callbackParameters;
for (auto parameter : fn->getParameters().getMembers())
Expand Down Expand Up @@ -1794,7 +1751,7 @@ data_map CGenerator::getFunctionTypeTemplateData(Group *group, FunctionType *fn)
{
data_map functionInfo = getFunctionTemplateData(group, function);
// set serverPrototype function with parameters of common function.
string serverProto = getFunctionServerCall(function, fn);
string serverProto = getFunctionServerCall(function, true);
functionInfo["serverPrototype"] = serverProto;
functionInfos.push_back(functionInfo);
}
Expand Down Expand Up @@ -1971,18 +1928,23 @@ string CGenerator::getErrorReturnValue(FunctionBase *fn)
}
}

string CGenerator::getFunctionServerCall(Function *fn, FunctionType *functionType, const std::string prefix)
string CGenerator::getFunctionServerCall(Function *fn, bool isCCall)
{
string proto = "";
if (!fn->getReturnType()->isVoid() && prefix.length() > 0)
if (!isCCall)
{
proto += "result = ";
if (!fn->getReturnType()->isVoid())
{
proto += "result = ";
}
proto += "m_handler->";
}
proto += prefix;
proto += getOutputName(fn);
proto += "(";

auto params = (functionType) ? functionType->getParameters().getMembers() : fn->getParameters().getMembers();
FunctionType *funcType = fn->getFunctionType();

auto params = (funcType) ? funcType->getParameters().getMembers() : fn->getParameters().getMembers();

if (params.size())
{
Expand All @@ -2004,7 +1966,7 @@ string CGenerator::getFunctionServerCall(Function *fn, FunctionType *functionTyp
(findAnnotation(it, SHARED_ANNOTATION))))

{
if (prefix != "")
if (!isCCall)
{
proto += "&";
}
Expand All @@ -2022,7 +1984,7 @@ string CGenerator::getFunctionServerCall(Function *fn, FunctionType *functionTyp
}

string CGenerator::getFunctionPrototype(Group *group, FunctionBase *fn, const std::string &interfaceName,
const std::string &name, bool interfaceClass)
const string &name, bool insideInterfaceCall)
{
DataType *dataTypeReturn = fn->getReturnType();
string proto = getExtraPointerInReturn(dataTypeReturn);
Expand All @@ -2037,12 +1999,11 @@ string CGenerator::getFunctionPrototype(Group *group, FunctionBase *fn, const st
ifaceVar += "::";
}

Symbol *symbol = dynamic_cast<Symbol *>(fn);
assert(symbol);

FunctionType *funType = dynamic_cast<FunctionType *>(fn);
if (name.empty())
{
Symbol *symbol = dynamic_cast<Symbol *>(fn);
assert(symbol);
string functionName = getOutputName(symbol);
if (funType) /* Need add '(*name)' for function type definition. */
{
Expand Down Expand Up @@ -2157,7 +2118,7 @@ string CGenerator::getFunctionPrototype(Group *group, FunctionBase *fn, const st
}
}

if (interfaceClass)
if (insideInterfaceCall)
{
if (trueDataType->isFunction())
{
Expand Down
13 changes: 7 additions & 6 deletions erpcgen/src/CGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class CGenerator : public Generator
*
* @return Contains interface function data.
*/
cpptempl::data_map getFunctionTemplateData(Group *group, Function *fn, Interface *interface = nullptr) override;
cpptempl::data_map getFunctionTemplateData(Group *group, Function *fn) override;

/*!
* @brief This function returns function type (callbacks type) template data.
Expand Down Expand Up @@ -469,23 +469,24 @@ class CGenerator : public Generator
*
* @param[in] group Group to which function belongs.
* @param[in] fn Function for prototyping.
* @param[in] name Name used for FunctionType.
* @param[in] interfaceClass interfaceClass specific.
* @param[in] interfaceName Interface name used for function declaration.
* @param[in] name Name used for shared code in case of function type.
* @param[in] insideInterfaceCall interfaceClass specific.
*
* @return String prototype representation for given function.
*/
std::string getFunctionPrototype(Group *group, FunctionBase *fn, const std::string &interfaceName = "",
const std::string &name = "", bool interfaceClass = false);
const std::string &name = "", bool insideInterfaceCall = false);

/*!
* @brief This function return interface function representation called by server side.
*
* @param[in] fn Function for interface function representation.
* @param[in] functionType Inside FunctionType common shim code server call need use FunctionType parameters names.
* @param[in] isCCall C and C++ code is similar, but not same.
*
* @return String representation for given function.
*/
std::string getFunctionServerCall(Function *fn, FunctionType *functionType = nullptr, const std::string = "");
std::string getFunctionServerCall(Function *fn, bool isCCall = false);

/*!
* @brief This function return name with guard.
Expand Down
15 changes: 6 additions & 9 deletions erpcgen/src/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,8 @@ data_list Generator::makeGroupInterfacesTemplateData(Group *group)
data_list callbacksInt;
data_list callbacksExt;
data_list callbacksAll;
getCallbacksTemplateData(iface, callbacksInt, callbacksExt, callbacksAll);
getCallbacksTemplateData(group, iface, callbacksInt, callbacksExt, callbacksAll);
ifaceInfo["callbacksInt"] = callbacksInt;
ifaceInfo["callbacksExt"] = callbacksExt;
ifaceInfo["callbacksAll"] = callbacksAll;
ifaceInfo["isNonExternalInterface"] = false;
for (unsigned int i = 0; i < functions.size(); ++i)
Expand Down Expand Up @@ -647,7 +646,7 @@ data_list Generator::getFunctionsTemplateData(Group *group, Interface *iface)
int j = 0;
for (auto fit : iface->getFunctions())
{
data_map function = getFunctionTemplateData(group, fit, iface);
data_map function = getFunctionTemplateData(group, fit);
fns.push_back(function);

Log::info(" %d: (%d) %s\n", j, fit->getUniqueId(), function["prototype"]->getvalue().c_str());
Expand All @@ -671,7 +670,7 @@ Generator::datatype_vector_t Generator::getDataTypesFromSymbolScope(SymbolScope
return vector;
}

void Generator::getCallbacksTemplateData(const Interface *iface, data_list &callbackTypesInt,
void Generator::getCallbacksTemplateData(Group *group, const Interface *iface, data_list &callbackTypesInt,
data_list &callbackTypesExt, data_list &callbackTypesAll)
{
list<FunctionType *> callbackTypes;
Expand Down Expand Up @@ -736,14 +735,12 @@ void Generator::getCallbacksTemplateData(const Interface *iface, data_list &call
{
data_map callbackType;
callbackType["name"] = functionType->getName();
AliasType aliasTypeInterface(getFunctionPrototype(nullptr, functionType, iface->getName() + "_interface"),
functionType);
AliasType aliasType(getFunctionPrototype(nullptr, functionType), functionType);
callbackType["typenameName"] = getOutputName(&aliasType);
callbackType["interfaceTypenameName"] = getOutputName(&aliasTypeInterface);
callbackType["typenameName"] = getFunctionPrototype(nullptr, functionType);
callbackType["interfaceTypenameName"] = getFunctionPrototype(nullptr, functionType, iface->getName() + "_interface");
if (!functionsInt.empty())
{
callbackType["callbacks"] = functionsInt;
callbackType["callbacksData"] = getFunctionTypeTemplateData(group, functionType);
callbackTypesInt.push_back(callbackType);
}
if (!functionsExt.empty())
Expand Down
36 changes: 29 additions & 7 deletions erpcgen/src/Generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,24 @@ class Generator
*
* @param[in] group Pointer to a group.
* @param[in] fn From this are set interface function template data.
* @param[in] fnIndex Function index.
*
* @return Contains interface function data.
*/
virtual cpptempl::data_map getFunctionTemplateData(Group *group, Function *fn, Interface *interface = nullptr) = 0;
virtual cpptempl::data_map getFunctionTemplateData(Group *group, Function *fn) = 0;

/*!
* @brief This function returns function type (callbacks type) template data.
*
* This function returns function type (callbacks type) template data with all data, which
* are necessary for generating output code for output files. Shim code is generating
* common function for serialization/deserialization of data.
*
* @param[in] group Group to which function belongs.
* @param[in] fn From this are set function type template data.
*
* @return Contains interface function data.
*/
virtual cpptempl::data_map getFunctionTypeTemplateData(Group *group, FunctionType *fn) = 0;

/*!
* @brief This function will get symbol comments and convert to language specific ones
Expand Down Expand Up @@ -250,7 +263,6 @@ class Generator
* @brief This function generates output files for defined interfaces.
*
* @param[in] group Pointer to a group.
* @param[out] commonFilesFilename Common filename part of group generated files.
*/
void generateGroupOutputFiles(Group *group);

Expand Down Expand Up @@ -361,13 +373,14 @@ class Generator
*
* @param[in] group Group to which function belongs.
* @param[in] fn Function for prototyping.
* @param[in] name Name used for FunctionType.
* @param[in] interfaceClass interfaceClass specific.
* @param[in] interfaceName Interface name used for function declaration.
* @param[in] name Name used for shared code in case of function type.
* @param[in] insideInterfaceCall interfaceClass specific.
*
* @return String prototype representation for given function.
*/
virtual std::string getFunctionPrototype(Group *group, FunctionBase *fn, const std::string &interfaceName = "",
const std::string &name = "", bool interfaceClass = false) = 0;
const std::string &name = "", bool insideInterfaceCall = false) = 0;

private:
/*!
Expand All @@ -382,7 +395,16 @@ class Generator
*/
cpptempl::data_list getFunctionsTemplateData(Group *group, Interface *iface);

void getCallbacksTemplateData(const Interface *iface, cpptempl::data_list &callbackTypesInt,
/*!
* @brief Get the Callbacks template data and dived them to the interface scope list.
*
* @param[in] group Group to which callbacks belongs.
* @param[in] iface Use callbacks belongs to this interface.
* @param[out] callbackTypesInt Template data for current interface scope callbacks
* @param[out] callbackTypesExt Template data for others interface scope callbacks
* @param[out] callbackTypesAll Template data of all callbacks.
*/
void getCallbacksTemplateData(Group *group, const Interface *iface, cpptempl::data_list &callbackTypesInt,
cpptempl::data_list &callbackTypesExt, cpptempl::data_list &callbackTypesAll);
};

Expand Down
32 changes: 2 additions & 30 deletions erpcgen/src/PythonGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ void PythonGenerator::generate()

makeEnumsTemplateData();

makeFunctionsTemplateData();

for (Group *group : m_groups)
{
data_map groupTemplate;
Expand All @@ -169,7 +167,7 @@ void PythonGenerator::setTemplateComments(Symbol *symbol, data_map &symbolInfo)
symbolInfo["ilComment"] = convertComment(symbol->getIlComment(), kInlineComment);
}

data_map PythonGenerator::getFunctionTemplateData(Group *group, Function *fn, Interface *interface)
data_map PythonGenerator::getFunctionTemplateData(Group *group, Function *fn)
{
(void)group;
data_map info;
Expand Down Expand Up @@ -267,7 +265,7 @@ data_map PythonGenerator::getFunctionTemplateData(Group *group, Function *fn, In
}

string PythonGenerator::getFunctionPrototype(Group *group, FunctionBase *fn, const string &interfaceName,
const string &name, bool interfaceClass)
const string &name, bool insideInterfaceCall)
{
FunctionType *functionType = dynamic_cast<FunctionType *>(fn);
if (functionType)
Expand Down Expand Up @@ -582,32 +580,6 @@ void PythonGenerator::setOneStructMemberTemplateData(StructMember *member, data_
setTemplateComments(member, member_info);
}

void PythonGenerator::makeFunctionsTemplateData()
{
/* type definitions of functions and table of functions */
Log::info("Functions:\n");
data_list functions;
for (Symbol *functionTypeSymbol : getDataTypesFromSymbolScope(m_globals, DataType::kFunctionType))
{
FunctionType *functionType = dynamic_cast<FunctionType *>(functionTypeSymbol);
data_map functionInfo;

/* Table template data. */
data_list callbacks;
for (Function *fun : functionType->getCallbackFuns())
{
data_map callbacksInfo;
callbacksInfo["name"] = fun->getName();
callbacks.push_back(callbacksInfo);
}
functionInfo["callbacks"] = callbacks;
/* Function type name. */
functionInfo["name"] = functionType->getName();
functions.push_back(functionInfo);
}
m_templateData["functions"] = functions;
}

data_map PythonGenerator::getTypeInfo(DataType *t)
{
data_map info;
Expand Down
Loading

0 comments on commit 40ae66b

Please sign in to comment.