aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/include
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 /c_compiler/include
parente065e781448b86eeeca0152f735649ea2a2edbb6 (diff)
downloadCompiler-c83c8f224e66d7e21e30546bae308ac5fd52677e.tar.gz
Compiler-c83c8f224e66d7e21e30546bae308ac5fd52677e.zip
Added shared_ptr for less memory leaks
Diffstat (limited to 'c_compiler/include')
-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
7 files changed, 66 insertions, 27 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);