Skip to content

Commit bf300b4

Browse files
authored
Merge pull request #128 from kornilova-l/function-parameters-shared_ptr
Store parameters in Function as vector of std::shared_ptr
2 parents 0161dcc + 052a0df commit bf300b4

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

bindgen/ir/Function.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Parameter::Parameter(std::string name, std::shared_ptr<Type> type)
66
: TypeAndName(std::move(name), type) {}
77

8-
Function::Function(const std::string &name, std::vector<Parameter *> parameters,
8+
Function::Function(const std::string &name,
9+
std::vector<std::shared_ptr<Parameter>> parameters,
910
std::shared_ptr<Type> retType, bool isVariadic)
1011
: name(name), scalaName(name), parameters(std::move(parameters)),
1112
retType(std::move(retType)), isVariadic(isVariadic) {}
@@ -31,12 +32,12 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
3132
}
3233

3334
bool Function::usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
34-
if (*retType == *type || retType.get()->usesType(type, stopOnTypeDefs)) {
35+
if (*retType == *type || retType->usesType(type, stopOnTypeDefs)) {
3536
return true;
3637
}
3738
for (const auto &parameter : parameters) {
3839
if (*parameter->getType() == *type ||
39-
parameter->getType().get()->usesType(type, stopOnTypeDefs)) {
40+
parameter->getType()->usesType(type, stopOnTypeDefs)) {
4041
return true;
4142
}
4243
}
@@ -67,12 +68,6 @@ void Function::setScalaName(std::string scalaName) {
6768
this->scalaName = std::move(scalaName);
6869
}
6970

70-
Function::~Function() {
71-
for (const auto &parameter : parameters) {
72-
delete parameter;
73-
}
74-
}
75-
7671
bool Function::isLegalScalaNativeFunction() const {
7772
/* Return type and parameters types cannot be array types because array type
7873
* in this case is always represented as a pointer to element type */

bindgen/ir/Function.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ class Parameter : public TypeAndName {
1414

1515
class Function {
1616
public:
17-
Function(const std::string &name, std::vector<Parameter *> parameters,
17+
Function(const std::string &name,
18+
std::vector<std::shared_ptr<Parameter>> parameters,
1819
std::shared_ptr<Type> retType, bool isVariadic);
1920

20-
~Function();
21-
2221
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2322
const Function &func);
2423

@@ -41,7 +40,7 @@ class Function {
4140

4241
std::string name; // real name of the function
4342
std::string scalaName; // not empty
44-
std::vector<Parameter *> parameters;
43+
std::vector<std::shared_ptr<Parameter>> parameters;
4544
std::shared_ptr<Type> retType;
4645
bool isVariadic;
4746
};

bindgen/ir/IR.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ IR::IR(std::string libName, std::string linkName, std::string objectName,
77
objectName(std::move(objectName)), locationManager(locationManager),
88
packageName(std::move(packageName)) {}
99

10-
void IR::addFunction(std::string name, std::vector<Parameter *> parameters,
10+
void IR::addFunction(std::string name,
11+
std::vector<std::shared_ptr<Parameter>> parameters,
1112
std::shared_ptr<Type> retType, bool isVariadic) {
1213
functions.push_back(std::make_shared<Function>(name, std::move(parameters),
1314
retType, isVariadic));

bindgen/ir/IR.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class IR {
2020

2121
~IR();
2222

23-
void addFunction(std::string name, std::vector<Parameter *> parameters,
23+
void addFunction(std::string name,
24+
std::vector<std::shared_ptr<Parameter>> parameters,
2425
std::shared_ptr<Type> retType, bool isVariadic);
2526

2627
std::shared_ptr<TypeDef> addTypeDef(std::string name,

bindgen/visitor/TreeVisitor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "TreeVisitor.h"
22
#include "clang/AST/RecordLayout.h"
3-
#include <stdio.h>
43

54
bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
65
if (!astContext->getSourceManager().isInMainFile(func->getLocation())) {
@@ -10,7 +9,7 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
109
std::string funcName = func->getNameInfo().getName().getAsString();
1110
std::shared_ptr<Type> retType =
1211
typeTranslator.translate(func->getReturnType());
13-
std::vector<Parameter *> parameters;
12+
std::vector<std::shared_ptr<Parameter>> parameters;
1413

1514
int anonCounter = 0;
1615

@@ -23,7 +22,7 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
2322
}
2423

2524
std::shared_ptr<Type> ptype = typeTranslator.translate(parm->getType());
26-
parameters.emplace_back(new Parameter(pname, ptype));
25+
parameters.emplace_back(std::make_shared<Parameter>(pname, ptype));
2726
}
2827

2928
ir.addFunction(funcName, std::move(parameters), retType,

0 commit comments

Comments
 (0)