aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-11 15:57:20 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-11 15:57:20 +0000
commitc83c8f224e66d7e21e30546bae308ac5fd52677e (patch)
tree9a15588353036cf2fccfbfb99bf934b3d1f3cbef
parente065e781448b86eeeca0152f735649ea2a2edbb6 (diff)
downloadCompiler-c83c8f224e66d7e21e30546bae308ac5fd52677e.tar.gz
Compiler-c83c8f224e66d7e21e30546bae308ac5fd52677e.zip
Added shared_ptr for less memory leaks
-rw-r--r--c_compiler/include/bindings.hpp7
-rw-r--r--c_compiler/include/declaration.hpp20
-rw-r--r--c_compiler/include/expression.hpp13
-rw-r--r--c_compiler/include/function.hpp15
-rw-r--r--c_compiler/include/statement.hpp23
-rw-r--r--c_compiler/include/translation_unit.hpp6
-rw-r--r--c_compiler/include/type.hpp9
-rw-r--r--c_compiler/src/bindings.cpp2
-rw-r--r--c_compiler/src/c_parser.y2
-rw-r--r--c_compiler/src/compiler_main.cpp2
-rw-r--r--c_compiler/src/declaration.cpp13
-rw-r--r--c_compiler/src/expression.cpp11
-rw-r--r--c_compiler/src/function.cpp2
-rw-r--r--c_compiler/src/statement.cpp10
-rw-r--r--c_compiler/src/translation_unit.cpp4
15 files changed, 87 insertions, 52 deletions
diff --git a/c_compiler/include/bindings.hpp b/c_compiler/include/bindings.hpp
index 9ca862f..983a10d 100644
--- a/c_compiler/include/bindings.hpp
+++ b/c_compiler/include/bindings.hpp
@@ -3,15 +3,18 @@
#include <cstdint>
#include <map>
+#include <memory>
#include <string>
class Type;
+typedef std::shared_ptr<Type> TypePtr;
+
// struct containing information on the variable declaration
struct DeclarationData
{
- Type* type;
+ TypePtr type;
int32_t stack_position;
};
@@ -26,7 +29,7 @@ private:
public:
VariableStackBindings();
- void insertBinding(std::string id, Type* type, int32_t stack_position);
+ void insertBinding(std::string id, TypePtr type, int32_t stack_position);
void increaseStackPosition();
int32_t getCurrentStackPosition() const;
diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp
index 5780bd9..08fd641 100644
--- a/c_compiler/include/declaration.hpp
+++ b/c_compiler/include/declaration.hpp
@@ -3,16 +3,24 @@
#include "node.hpp"
+#include <memory>
+
class Expression;
+class Type;
+class Declaration;
+
+typedef std::shared_ptr<Expression> ExpressionPtr;
+typedef std::shared_ptr<Type> TypePtr;
+typedef std::shared_ptr<Declaration> DeclarationPtr;
class Declaration : public Node {
private:
- Type* type;
+ TypePtr type;
std::string id;
- Expression* init;
- Declaration* next_decl;
- Declaration* list_next_decl;
+ ExpressionPtr init;
+ DeclarationPtr next_decl;
+ DeclarationPtr list_next_decl;
public:
Declaration(const std::string& _id = "", Expression* _init = nullptr);
@@ -26,8 +34,8 @@ public:
void setType(Type* _type);
- Declaration* getNext() const;
- Declaration* getNextListItem() const;
+ DeclarationPtr getNext() const;
+ DeclarationPtr getNextListItem() const;
std::string getId() const;
std::string getType() const;
};
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp
index 89a996e..df95a1e 100644
--- a/c_compiler/include/expression.hpp
+++ b/c_compiler/include/expression.hpp
@@ -3,12 +3,16 @@
#include "node.hpp"
+#include <memory>
+
+class Expression;
+
+typedef std::shared_ptr<Expression> ExpressionPtr;
+
class Expression : public Node
{
public:
- virtual ~Expression();
-
virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0;
virtual void print() const;
@@ -20,11 +24,10 @@ public:
class OperationExpression : public Expression
{
protected:
- Expression* lhs;
- Expression* rhs;
+ ExpressionPtr lhs;
+ ExpressionPtr rhs;
public:
OperationExpression(Expression* _lhs, Expression* _rhs);
- virtual ~OperationExpression();
virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0;
};
diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp
index 77d1372..57e0ac1 100644
--- a/c_compiler/include/function.hpp
+++ b/c_compiler/include/function.hpp
@@ -3,16 +3,25 @@
#include "node.hpp"
+#include <memory>
+
class Declaration;
class Statement;
+class Type;
+class Function;
+
+typedef std::shared_ptr<Declaration> DeclarationPtr;
+typedef std::shared_ptr<Statement> StatementPtr;
+typedef std::shared_ptr<Type> TypePtr;
+typedef std::shared_ptr<Function> FunctionPtr;
class Function : public Node {
protected:
- Type* type;
+ TypePtr type;
std::string id;
- Declaration* parameter_list;
- Statement* statement;
+ DeclarationPtr parameter_list;
+ StatementPtr statement;
public:
Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement);
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp
index b5f7e9c..d491b68 100644
--- a/c_compiler/include/statement.hpp
+++ b/c_compiler/include/statement.hpp
@@ -3,13 +3,20 @@
#include "node.hpp"
+#include <memory>
+
class Declaration;
class Expression;
+class Statement;
+
+typedef std::shared_ptr<Declaration> DeclarationPtr;
+typedef std::shared_ptr<Expression> ExpressionPtr;
+typedef std::shared_ptr<Statement> StatementPtr;
class Statement : public Node {
protected:
- Statement* next_statement;
+ StatementPtr next_statement;
public:
Statement(Statement* statement = nullptr);
@@ -26,8 +33,8 @@ public:
class CompoundStatement : public Statement {
protected:
- Declaration* m_decl;
- Statement* m_statement;
+ DeclarationPtr m_decl;
+ StatementPtr m_statement;
public:
CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr);
@@ -43,8 +50,8 @@ public:
class SelectionStatement : public Statement {
protected:
- Statement* m_if;
- Statement* m_else;
+ StatementPtr m_if;
+ StatementPtr m_else;
public:
SelectionStatement(Statement* _if = nullptr, Statement* _else = nullptr);
@@ -58,7 +65,7 @@ public:
class ExpressionStatement : public Statement {
protected:
- Expression* m_expr;
+ ExpressionPtr m_expr;
public:
ExpressionStatement(Expression* expr = nullptr);
@@ -72,7 +79,7 @@ public:
class JumpStatement : public Statement {
protected:
- Expression* m_expr;
+ ExpressionPtr m_expr;
public:
JumpStatement(Expression* expr = nullptr);
@@ -86,7 +93,7 @@ public:
class IterationStatement : public Statement {
protected:
- Statement* m_statement;
+ StatementPtr m_statement;
public:
IterationStatement(Statement* statement);
diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp
index 5c20855..75ee53b 100644
--- a/c_compiler/include/translation_unit.hpp
+++ b/c_compiler/include/translation_unit.hpp
@@ -3,11 +3,15 @@
#include "node.hpp"
+#include <memory>
#include <vector>
+typedef std::shared_ptr<Node> NodePtr;
+
+
class TranslationUnit : public Node {
protected:
- std::vector<Node* > translation_unit;
+ std::vector<NodePtr> translation_unit;
public:
TranslationUnit(Node* decl);
diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp
index 3deca58..608b660 100644
--- a/c_compiler/include/type.hpp
+++ b/c_compiler/include/type.hpp
@@ -3,6 +3,11 @@
#include "node.hpp"
+#include <memory>
+
+class Type;
+
+typedef std::shared_ptr<Type> TypePtr;
class Type : public Node {
public:
@@ -22,7 +27,7 @@ public:
class Pointer : public Type {
protected:
- Type* pointer_type;
+ TypePtr pointer_type;
public:
Pointer(Type* _pointer_type);
@@ -34,7 +39,7 @@ public:
class Array : public Type {
protected:
int32_t size;
- Type* array_type;
+ TypePtr array_type;
public:
Array(Type* _array_type, int32_t _size = 0);
diff --git a/c_compiler/src/bindings.cpp b/c_compiler/src/bindings.cpp
index e920783..4315407 100644
--- a/c_compiler/src/bindings.cpp
+++ b/c_compiler/src/bindings.cpp
@@ -7,7 +7,7 @@ VariableStackBindings::VariableStackBindings()
: stack_counter(4)
{}
-void VariableStackBindings::insertBinding(std::string id, Type* type, int32_t stack_position)
+void VariableStackBindings::insertBinding(std::string id, TypePtr type, int32_t stack_position)
{
DeclarationData decl_data;
decl_data.type = type;
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index dd609ee..d36c5ca 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -124,7 +124,7 @@ Declaration:
while(tmp_decl != nullptr) {
tmp_decl->setType($1);
- tmp_decl = tmp_decl->getNextListItem();
+ tmp_decl = tmp_decl->getNextListItem().get();
}
};
diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp
index 69504c9..dc243e0 100644
--- a/c_compiler/src/compiler_main.cpp
+++ b/c_compiler/src/compiler_main.cpp
@@ -7,7 +7,7 @@ Node* parseAST();
int main(int argc, char *argv[])
{
- Node* ast = parseAST();
+ std::unique_ptr<Node> ast(parseAST());
VariableStackBindings bindings;
diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp
index 9eff776..dfe6158 100644
--- a/c_compiler/src/declaration.cpp
+++ b/c_compiler/src/declaration.cpp
@@ -71,25 +71,28 @@ VariableStackBindings Declaration::printasm(VariableStackBindings bindings) cons
void Declaration::addDeclaration(Declaration* _next_decl)
{
- next_decl = _next_decl;
+ DeclarationPtr decl_ptr(_next_decl);
+ next_decl = decl_ptr;
}
void Declaration::addList(Declaration* _next_decl)
{
- list_next_decl = _next_decl;
+ DeclarationPtr decl_ptr(_next_decl);
+ list_next_decl = decl_ptr;
}
void Declaration::setType(Type* _type)
{
- type = _type;
+ TypePtr type_ptr(_type);
+ type = type_ptr;
}
-Declaration* Declaration::getNext() const
+DeclarationPtr Declaration::getNext() const
{
return next_decl;
}
-Declaration* Declaration::getNextListItem() const
+DeclarationPtr Declaration::getNextListItem() const
{
return list_next_decl;
}
diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp
index cd20e24..6c1ba97 100644
--- a/c_compiler/src/expression.cpp
+++ b/c_compiler/src/expression.cpp
@@ -5,10 +5,6 @@
// Expression definition
-// There are no values to delete so it is just empty
-Expression::~Expression()
-{}
-
void Expression::print() const
{
std::cerr << "This expression has not been implemented yet" << std::endl;
@@ -34,13 +30,6 @@ OperationExpression::OperationExpression(Expression* _lhs, Expression* _rhs)
: lhs(_lhs), rhs(_rhs)
{}
-// deletes the two member variables that have been initialized
-OperationExpression::~OperationExpression()
-{
- delete lhs;
- delete rhs;
-}
-
// Assignment Expression definition
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp
index 6c41c7a..1d9db33 100644
--- a/c_compiler/src/function.cpp
+++ b/c_compiler/src/function.cpp
@@ -28,7 +28,7 @@ void Function::printxml() const
{
std::cout << "<Function id=\"" << id << "\">" << std::endl;
- Declaration* parameter = parameter_list;
+ DeclarationPtr parameter = parameter_list;
std::vector<std::string> parameter_vec;
while(parameter != nullptr) {
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp
index 50217fb..fa85df0 100644
--- a/c_compiler/src/statement.cpp
+++ b/c_compiler/src/statement.cpp
@@ -14,7 +14,8 @@ Statement::Statement(Statement* statement)
void Statement::addStatement(Statement* _next)
{
- next_statement = _next;
+ StatementPtr statement_ptr(_next);
+ next_statement = statement_ptr;
}
@@ -71,7 +72,7 @@ VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings
void CompoundStatement::count_variables(int32_t& var_count) const
{
- Declaration* declaration = m_decl;
+ DeclarationPtr declaration = m_decl;
if(next_statement != nullptr)
next_statement->count_variables(var_count);
@@ -80,7 +81,7 @@ void CompoundStatement::count_variables(int32_t& var_count) const
m_statement->count_variables(var_count);
while(declaration != nullptr) {
- Declaration* declaration_list = declaration->getNextListItem();
+ DeclarationPtr declaration_list = declaration->getNextListItem();
while(declaration_list != nullptr) {
var_count++;
@@ -110,8 +111,10 @@ void SelectionStatement::printxml() const
{
if(next_statement != nullptr)
next_statement->printxml();
+
if(m_if != nullptr)
m_if->printxml();
+
if(m_else != nullptr)
m_else->printxml();
}
@@ -210,6 +213,7 @@ void IterationStatement::printxml() const
{
if(next_statement != nullptr)
next_statement->printxml();
+
if(m_statement != nullptr)
m_statement->printxml();
}
diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp
index 4bddac2..1d09410 100644
--- a/c_compiler/src/translation_unit.cpp
+++ b/c_compiler/src/translation_unit.cpp
@@ -3,7 +3,6 @@
#include <iostream>
-
// Translation Unit definition
TranslationUnit::TranslationUnit(Node* decl)
@@ -38,5 +37,6 @@ VariableStackBindings TranslationUnit::printasm(VariableStackBindings bindings)
void TranslationUnit::push(Node* decl)
{
- translation_unit.push_back(decl);
+ NodePtr node_ptr(decl);
+ translation_unit.push_back(node_ptr);
}